Skip to content

SQLite queue database corruption #324

Closed

Description

@andycroll

I'm seeing a strange error. I dropped @fractaledmind a note and he suggested I post here.

W, [2024-09-05T18:49:32.074281 #809854]  WARN -- : SolidQueue-0.3.4 Error deregistering Supervisor (1.1ms)  process_id: 68, pid: 564289, hostname: "HOST", last_heartbeat_at: Sat, 31 Aug 2024 15:16:55.474066000 UTC +00:00, claimed_size: 0, pruned: {:process=>#<SolidQueue::Process id: 68>, :pruned=>true, :claimed_size=>0, :error=>#<ActiveRecord::StatementInvalid: SQLite3::CorruptException: database disk image is malformed>, :exception=>["ActiveRecord::StatementInvalid", "SQLite3::CorruptException: database disk image is malformed"], :exception_object=>#<ActiveRecord::StatementInvalid: SQLite3::CorruptException: database disk image is malformed>}, error: "ActiveRecord::StatementInvalid SQLite3::CorruptException: database disk image is malformed"

In sqlite3 client.

sqlite> .open production-queue.sqlite3
sqlite> pragma integrity_check;
*** in database main ***
On tree page 523 cell 23: Rowid 146 out of order
NULL value in solid_queue_jobs.created_at
NULL value in solid_queue_jobs.updated_at
row 146 missing from index index_solid_queue_jobs_for_alerting
row 146 missing from index index_solid_queue_jobs_for_filtering
row 146 missing from index index_solid_queue_jobs_on_finished_at
row 146 missing from index index_solid_queue_jobs_on_active_job_id
row 146 missing from index index_solid_queue_jobs_on_class_name
NULL value in solid_queue_jobs.created_at
NULL value in solid_queue_jobs.updated_at
row 147 missing from index index_solid_queue_jobs_for_alerting
row 147 missing from index index_solid_queue_jobs_for_filtering
row 147 missing from index index_solid_queue_jobs_on_finished_at
row 147 missing from index index_solid_queue_jobs_on_active_job_id
row 147 missing from index index_solid_queue_jobs_on_class_name
NULL value in solid_queue_jobs.created_at
NULL value in solid_queue_jobs.updated_at
row 148 missing from index index_solid_queue_jobs_for_alerting
row 148 missing from index index_solid_queue_jobs_for_filtering
row 148 missing from index index_solid_queue_jobs_on_finished_at
row 148 missing from index index_solid_queue_jobs_on_active_job_id
row 148 missing from index index_solid_queue_jobs_on_class_name
NULL value in solid_queue_jobs.created_at
NULL value in solid_queue_jobs.updated_at
row 149 missing from index index_solid_queue_jobs_for_alerting
row 149 missing from index index_solid_queue_jobs_for_filtering
row 149 missing from index index_solid_queue_jobs_on_finished_at
row 149 missing from index index_solid_queue_jobs_on_active_job_id
row 149 missing from index index_solid_queue_jobs_on_class_name
Error: stepping, database disk image is malformed (11)
sqlite> pragma integrity_check;
*** in database main ***
On tree page 523 cell 23: Rowid 146 out of order
NULL value in solid_queue_jobs.created_at
NULL value in solid_queue_jobs.updated_at
row 146 missing from index index_solid_queue_jobs_for_alerting
row 146 missing from index index_solid_queue_jobs_for_filtering
row 146 missing from index index_solid_queue_jobs_on_finished_at
row 146 missing from index index_solid_queue_jobs_on_active_job_id
row 146 missing from index index_solid_queue_jobs_on_class_name
Error: stepping, database disk image is malformed (11)

Doing a .clone doesn't seem to fix.

I'm not in "real" production and this is a job queue and thus somewhat destroyable, so I'm going to take that approach.

I've also seen similar corruption on my local development environment too akin to that mentioned by @marcoroth on X.

https://x.com/marcoroth_/status/1831045885423497223

In that case I just destroyed and recreated the file and moved on.

Activity

  1. marcoroth commented on Sep 5, 2024

    @marcoroth

    Yes, this is excactly what I was seeing as well. Thanks for opening the issue and tagging me @andycroll as I wanted to open an issue for this too 🙌🏼

    This happened to me in this open source repo: http://github.com/adrienpoly/rubyvideo

    You can easily reproduce it by running rails s as a separate process and doing a double Ctrl-C in quick succession.

    I think it only happens with the Puma Plugin from what I could tell.

  2. andycroll commented on Sep 5, 2024

    @andycroll
    Author

    In production, I'm running as a separate worker, not the puma plugin.

  3. rosa commented on Sep 5, 2024

    @rosa
    Member

    Ohhh! See also #294, where @wdiechmann provided some advice in the last comment, which I'm not sure if it applies to your case.

    The Puma plugin detail is quite interesting! Did you manage to reproduce only with that?

  4. rosa commented on Sep 5, 2024

    @rosa
    Member

    Ah, sorry, just reread and I see @andycroll's error happened when running just the supervisor separately 🤔
    Do you know what happened right before that?

  5. andycroll commented on Sep 5, 2024

    @andycroll
    Author

    In my case a deploy (on a Hatchbox managed server) so killing the worker process was certainly happening when the error started.

  6. rosa commented on Sep 5, 2024

    @rosa
    Member

    It's also very surprising to me that you can corrupt a database with only Active Record and quite regular queries 🤔

    *** in database main ***
    On tree page 523 cell 23: Rowid 146 out of order
    NULL value in solid_queue_jobs.created_at
    NULL value in solid_queue_jobs.updated_at
    row 146 missing from index index_solid_queue_jobs_for_alerting
    row 146 missing from index index_solid_queue_jobs_for_filtering
    row 146 missing from index index_solid_queue_jobs_on_finished_at
    row 146 missing from index index_solid_queue_jobs_on_active_job_id
    row 146 missing from index index_solid_queue_jobs_on_class_name
    NULL value in solid_queue_jobs.created_at
    NULL value in solid_queue_jobs.updated_at
    

    I wouldn't know how to cause this on purpose 😅 A row missing from an index? 🤔

    I'll use @marcoroth's instructions to reproduce and see if I can find out what's going on!

  7. fractaledmind commented on Sep 5, 2024

    @fractaledmind

    I haven't been able to dig in myself yet, but after reading this: https://www.sqlite.org/howtocorrupt.html and knowing the bits that I know about the supervisor architecture, I was first drawn to:

    2.6. Carrying an open database connection across a fork()

  8. marcoroth commented on Sep 5, 2024

    @marcoroth

    If it helps, here's how my pragma integrity_check looks like:

    ❯ sqlite3
    SQLite version 3.43.2 2023-10-10 13:08:14
    Enter ".help" for usage hints.
    Connected to a transient in-memory database.
    Use ".open FILENAME" to reopen on a persistent database.
    sqlite> .open ./storage/development-queue.sqlite3
    sqlite> pragma integrity_check;
    row 1 missing from index index_solid_queue_processes_on_supervisor_id
    row 2 missing from index index_solid_queue_processes_on_supervisor_id
    row 3 missing from index index_solid_queue_processes_on_supervisor_id
    wrong # of entries in index index_solid_queue_processes_on_supervisor_id
    sqlite>
    
  9. rosa commented on Sep 5, 2024

    @rosa
    Member

    Ohhhh, thanks a lot @fractaledmind. That'd make perfect sense as the culprit! I thought Rails would handle this properly and wouldn't reuse the same connection but I need to dig more.

  10. fractaledmind commented on Sep 5, 2024

    @fractaledmind

    Yeah, this sounds like it might be what is occurring:

    Do not even call sqlite3_close() on a database connection from a child process if the connection was opened in the parent. It is safe to close the underlying file descriptor, but the sqlite3_close() interface might invoke cleanup activities that will delete content out from under the parent, leading to errors and perhaps even database corruption.

  11. rosa commented on Sep 6, 2024

    @rosa
    Member

    I thought Rails would handle this properly and wouldn't reuse the same connection but I need to dig more.

    The reason I thought that were these PRs that I had looked at in the past, because of another error (not as serious as this one) that someone reported with PostgreSQL 🤔

    There must be something else going on here.

  12. fractaledmind commented on Sep 6, 2024

    @fractaledmind

    No idea yet if this is related, but rails/rails#31173 has this in the description:

    I think sqlite3 is immune to this problem just because there is no server to confuse.

    And no discard! method is defined (in that PR or currently) for the SQLite3 AR adapter

  13. rosa commented on Sep 6, 2024

    @rosa
    Member

    Thanks @fractaledmind! I see this code has changed quite a lot since these PRs, (for example rails/rails#37296), so I was trying to understand how everything worked... That comment caught my attention too but I honestly wasn't sure why "there is no server to confuse" makes SQLite immune to this problem 😳

    @marcoroth, I've been trying to reproduce the problem following your steps but I can't. I have to say I have upgraded Solid Queue in rubyvideo to version 0.8.2 as it was using a quite old version so maybe there's something else at play there.

  14. rosa commented on Sep 6, 2024

    @rosa
    Member

    I have to say I have upgraded Solid Queue in rubyvideo to version 0.8.2 as it was using a quite old version so maybe there's something else at play there.

    Tried without upgrading and same thing... I can't manage to reproduce it by starting the app with rails s and then Ctrl + C, double, in quick succession 😕

  15. rosa commented on Sep 6, 2024

    @rosa
    Member

    I've also tried to artificially delay the jobs that are being run (in particular, MeiliSearch::Rails::MSJob, that were failing because I don't have Meilisearch running, so they were failing very fast) by adding some sleeps there, and still same behaviour. I don't manage to corrupt the file.

  16. 59 remaining items

  17. flavorjones commented on Sep 16, 2024

    @flavorjones
    Member
  18. flavorjones commented on Sep 18, 2024

    @flavorjones
    Member

    @andycroll @marcoroth @fractaledmind I just cut a release candidate of sqlite3-ruby with the fork-safety changes from sparklemotion/sqlite3-ruby#558:

    https://github.com/sparklemotion/sqlite3-ruby/releases/tag/v2.1.0.rc1

    It's an invasive change, it's been a hard issue to reproduce, and it's challenging to test every possible scenario ... so I would really like to have some folks use it and see if we can find problems and/or build confidence.

    I've tried out the solid_queue_bughunt repo and it seems like it's working OK (and I can see the warning messages from sqlite3-ruby in the log which means it's doing something ✔). But the more scenarios we can test with the better. If you could just drop a note here if you've been able to test (or if you intend to test), I would ❤️ you forever.

  19. flavorjones commented on Sep 19, 2024

    @flavorjones
    Member
  20. adrienpoly commented on Sep 23, 2024

    @adrienpoly

    I've tried out the solid_queue_bughunt repo and it seems like it's working OK (and I can see the warning messages from sqlite3-ruby in the log which means it's doing something ✔). But the more scenarios we can test with the better. If you could just drop a note here if you've been able to test (or if you intend to test), I would ❤️ you forever.

    FYI I have upgraded rubyvideo.dev with this latest version(rubyevents/rubyevents@71ece9b). It is now live in prod. So fare everything looks perfect.

  21. npezza93 commented on Sep 24, 2024

    @npezza93
    Contributor

    Been using the new sqlite rc version and havent had any issues. But when running bin/jobs start i'm seeing:

    /Users/nick/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/sqlite3-2.1.0.rc2-arm64-darwin/lib/sqlite3/fork_safety.rb:41: warning: Writable sqlite database connection(s) were inherited from a forked process. This is unsafe and the connections are being closed to prevent possible data corruption. Please close writable sqlite database connections before forking.
    

    Should i be seeing that warning with this config?

    I have a database config of

    default: &default
      adapter: sqlite3
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      timeout: 5000
    
    development:
      primary:
        <<: *default
        database: storage/dev.sqlite3
    

    And a queue.yml of

    default: &default
      dispatchers:
        - polling_interval: 1
          batch_size: 500
      workers:
        - queues: "*"
          threads: 3
          processes: <%= ENV.fetch("JOB_CONCURRENCY", 2) %>
          polling_interval: 0.1
    
    development:
      <<: *default
    
    test:
      <<: *default
    
    production:
      <<: *default
    
  22. flavorjones commented on Sep 24, 2024

    @flavorjones
    Member

    @npezza93 I think it's normal to see this message. I'm not a solid queue expert, but I believe the supervisor process hits the database (as part of active record bootstrapping if nothing else) before forking, and that would result in a warning on your tty. Someone who knows it better might be able to tell me how I'm wrong.

    Sqlite3-ruby maintainers discussed whether or not to emit warnings, but we decided to err on the side of overcommunicating, at least in the RC and probably the first final release of 2.1.0.

  23. dhh commented on Sep 24, 2024

    @dhh
    Member

    @flavorjones Maybe we can set a flag for Rails 8 to suppress that warning? Because would be nice if out of the box there is nothing alarming posted about the default setup. Especially since we still have some ways to go to reassure folks that sqlite is good and safe in production.

  24. flavorjones commented on Sep 24, 2024

    @flavorjones
    Member

    @dhh Yep. We can add a method SQLite3::ForkSafety.suppress_warnings! that will hush it up, and it can be called in the sqlite3 adapter. Hang tight.

  25. flavorjones commented on Sep 24, 2024

    @flavorjones
    Member
  26. flavorjones commented on Sep 24, 2024

    @flavorjones
    Member
  27. dhh commented on Sep 24, 2024

    @dhh
    Member

    Perfect, @flavorjones 👌

  28. rosa commented on Sep 25, 2024

    @rosa
    Member

    I believe the supervisor process hits the database (as part of active record bootstrapping if nothing else) before forking

    Yes, that's exactly right! It uses the DB to register itself, which happens before forking. It's useful to have the supervisor registered before because, in that way, the other processes can register themselves knowing their supervisor_id (from registering the supervisor).

    Also, suppress the warnings related to forking with open writable connections, despite the lost
    memory per-connection.

    I haven't had time in the last few days, but I hope to address this in Solid Queue as well, closing the connections before forking.

  29. northeastprince commented on Sep 29, 2024

    @northeastprince
    Contributor
  30. dhh commented on Sep 29, 2024

    @dhh
    Member

    Yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions