solid_queue: `PG::ConnectionBad` error when restarting container running `solid_queue:start`

Hey there!

We are moving towards SolidQueue for our apps, we started with just one and have that one running in production for a while now.

One issue that we are seeing is a PG::ConnectionBad error being raised when the container is killed (either when deploying or nightly rebalancing of containers)

Is there anything we can do to stop this error from being raised? Ignoring it means we also ignore actual database connection issues when something goes wrong.

Any help with debugging this is greatly appreciated!


the error has this message:

PQconsumeInput() server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.

From what i can gather it looks like the supervisor trying to deregister maybe after the connection was closed already?

here is the full backtrace of the error
lib/active_record/connection_adapters/postgresql_adapter.rb:894 exec_params |  
lib/active_record/connection_adapters/postgresql_adapter.rb:894 block (2 levels) in exec_no_cache |  
lib/active_record/connection_adapters/abstract_adapter.rb:1028 block in with_raw_connection |  
lib/active_support/concurrency/null_lock.rb:9 synchronize |  
lib/active_record/connection_adapters/abstract_adapter.rb:1000 with_raw_connection |  
lib/active_record/connection_adapters/postgresql_adapter.rb:893 block in exec_no_cache |  
lib/active_support/notifications/instrumenter.rb:58 instrument |  
lib/active_record/connection_adapters/abstract_adapter.rb:1143 log |  
lib/active_record/connection_adapters/postgresql_adapter.rb:892 exec_no_cache |  
lib/active_record/connection_adapters/postgresql_adapter.rb:872 execute_and_clear |  
lib/active_record/connection_adapters/postgresql/database_statements.rb:64 internal_exec_query |  
lib/active_record/connection_adapters/abstract/database_statements.rb:630 select |  
lib/active_record/connection_adapters/abstract/database_statements.rb:71 select_all |  
lib/active_record/connection_adapters/abstract/query_cache.rb:115 select_all |  
lib/active_record/relation/calculations.rb:477 block in execute_simple_calculation |  
lib/active_record/relation.rb:1018 skip_query_cache_if_necessary |  
lib/active_record/relation/calculations.rb:476 execute_simple_calculation |  
lib/active_record/relation/calculations.rb:433 perform_calculation |  
lib/active_record/relation/calculations.rb:245 calculate |  
lib/active_record/relation/calculations.rb:102 count |  
lib/active_record/associations/has_many_association.rb:84 count_records |  
lib/active_record/associations/collection_association.rb:218 size |  
lib/active_record/associations/collection_proxy.rb:783 size |  
app/models/solid_queue/process.rb:28 deregister |  
lib/solid_queue/processes/registrable.rb:27 deregister |  
lib/active_support/callbacks.rb:403 block in make_lambda |  
lib/active_support/callbacks.rb:239 block in halting_and_conditional |  
lib/active_support/callbacks.rb:602 block in invoke_after |  
lib/active_support/callbacks.rb:602 each |  
lib/active_support/callbacks.rb:602 invoke_after |  
lib/active_support/callbacks.rb:111 run_callbacks |  
lib/solid_queue/supervisor.rb:33 start |  
lib/solid_queue/supervisor.rb:12 start |  
lib/solid_queue/tasks.rb:4 block (2 levels) in <top (required)> |  
lib/rake/task.rb:281 block in execute |  
lib/rake/task.rb:281 each |  
lib/rake/task.rb:281 execute |  
lib/appsignal/integrations/rake.rb:15 block in execute |  
lib/appsignal/transaction.rb:913 instrument |  
lib/appsignal/helpers/instrumentation.rb:995 instrument |  
lib/appsignal/integrations/rake.rb:14 execute |  
lib/rake/task.rb:219 block in invoke_with_call_chain |  
lib/rake/task.rb:199 synchronize |  
lib/rake/task.rb:199 invoke_with_call_chain |  
lib/rake/task.rb:188 invoke |  
lib/rake/application.rb:188 invoke_task |  
lib/rake/application.rb:138 block (2 levels) in top_level |  
lib/rake/application.rb:138 each |  
lib/rake/application.rb:138 block in top_level |  
lib/rake/application.rb:147 run_with_threads |  
lib/rake/application.rb:132 top_level |  
lib/rails/commands/rake/rake_command.rb:27 block (2 levels) in perform |  
lib/rake/application.rb:214 standard_exception_handling |  
lib/rails/commands/rake/rake_command.rb:27 block in perform |  
lib/rails/commands/rake/rake_command.rb:44 block in with_rake |  
lib/rake/rake_module.rb:59 with_application |  
lib/rails/commands/rake/rake_command.rb:41 with_rake |  
lib/rails/commands/rake/rake_command.rb:20 perform |  
lib/rails/command.rb:156 invoke_rake |  
lib/rails/command.rb:73 block in invoke |  
lib/rails/command.rb:149 with_argv |  
lib/rails/command.rb:69 invoke |  
lib/rails/commands.rb:18 <top (required)> |  
/usr/local/lib/ruby/3.3.0/bundled_gems.rb:74 require |  
/usr/local/lib/ruby/3.3.0/bundled_gems.rb:74 block (2 levels) in replace_require |  
bin/rails:4 <main>
here is the application log from boot to eventual restart
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | [2024-07-24T03:00:42 (process) #1][INFO] appsignal: Starting AppSignal 3.11.0 (bin/rails, Ruby 3.3.4, x86_64-linux-musl)
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | 2024-07-24 03:00:43.283382 I [T5600] Rails -- Loaded .env
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | 2024-07-24 03:00:51.482689 I [T5600] SolidQueue -- SolidQueue-0.3.4 Register Supervisor (210.7ms)  pid: 1, hostname: "staging-docker-worker-3:gate_worker_1"
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | 2024-07-24 03:00:51.571082 W [T5600] SolidQueue -- SolidQueue-0.3.4 Tried to replace forked process but it had already died (0.0ms)  pid: 11, status: 0, pid_from_status: 11, signaled: false, stopsig: nil, termsig: nil
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | 2024-07-24 03:05:53.177324 I [Tio-worker-1] SolidQueue -- SolidQueue-0.3.4 Deregister Supervisor (87.6ms)  process_id: 245, pid: 1, hostname: "staging-docker-worker-3:gate_worker_1", last_heartbeat_at: Wed, 24 Jul 2024 02:59:30.760765000 CEST +02:00, claimed_size: 0, pruned: {:process=>#<SolidQueue::Process id: 245, kind: "Supervisor", last_heartbeat_at: "2024-07-24 02:59:30.760765000 +0200", supervisor_id: nil, pid: 1, hostname: "staging-docker-worker-3:gate_worker_1", metadata: {}, created_at: "2024-07-23 15:58:24.050132000 +0200">, :pruned=>true, :claimed_size=>0}

gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | 2024-07-24 12:48:42.267770 I [T5600] SolidQueue -- SolidQueue-0.3.4 Supervisor terminated gracefully (625.4ms)  supervisor_pid: 1, supervised_pids: [28, 31]
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | bin/rails aborted!
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | ActiveRecord::ConnectionFailed: PQconsumeInput() server closed the connection unexpectedly (ActiveRecord::ConnectionFailed)
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | 	This probably means the server terminated abnormally
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | 	before or while processing the request.
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    |
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    |
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | Caused by:
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | PG::ConnectionBad: PQconsumeInput() server closed the connection unexpectedly (PG::ConnectionBad)
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | 	This probably means the server terminated abnormally
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | 	before or while processing the request.
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    |
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | Tasks: TOP => solid_queue:start
gate_worker.1.bxrxeomly8pa8gvvvdxot19pe@staging-docker-worker-3    | (See full trace by running task with --trace)

Here is our configuration

  • tried to rescue the error with on thread error but that doesn’t catch it
  • tried the pidfile because we are seeing a warning on start: W [T6860] SolidQueue -- SolidQueue-0.3.4 Tried to replace forked process but it had already died (0.0ms) pid: 13, status: 0, pid_from_status: 13, signaled: false, stopsig: nil, termsig: nil this warning may be the result of the error in the deregister call now that i think about it.
  SolidQueue.supervisor_pidfile = "/tmp/solid_queue_supervisor.pid"
  SolidQueue.on_thread_error = proc do |error|
    case error
    when ActiveRecord::ConnectionFailed
      Rails.logger.error "SolidQueue: #{error.message}"
    else
      Rails.error.report(exception, handled: false)
    end
  end

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 17 (9 by maintainers)

Most upvoted comments

Ohh, great news! We’ve been running that branch also for quite a few days in production for HEY and everything has worked well, so I’m going to merge that now and ship a new version. I’ll keep this one open in case you run into this error again.

Thanks a lot again! 🙏

@rosa We’ve been running #269 on one of our apps for the entire weekend and haven’t seen the error yet! It has definitely been rebooted a few times and just now manually too. very smooth now!

Hey! I will try this out soon!

is it via Docker’s CMD?

yup!

Is it just bin/rails solid_queue:start?

yup!

oh I’ve been testing a combination of SIGKILL and SIGTERM, they both sometimes trigger the exception and sometimes not…

manually killing worker and dispatcher processes work fine and the supervisor starts new ones like it’s supposed to. manually killing the supervisor (both term and kill sometimes trigger the error and sometimes not.

the manual killing was done in a shell inside the container

however in our production setup the restarting is managed by docker so the 10 second (now 20) wait before the final sigkill

Looking forward to try those changes!

Hey @InteNs, thanks for the detailed report. I’ll look into this!