Why does the Ruby driver not wait for server selection during failover?

We operate 5 replicas on MongoDB Atlas. Set retryWrites = true in Ruby client connection string.
The problem is that when I shut down the primary member (for example, maintenance), I expect the read retry to be started (the read has failed once) and the driver wants to wait for the primary election, the driver will retry without waiting.

I want the driver to wait for the selected primary, how do I solve it?

"2020-02-06T04:51:42.112+09:00" Read retry due to: Mongo::Error::SocketError Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (server-00-01-ismg4.mongodb.net:27017, TLS)) (on server-00-01-ismg4.mongodb.net:27017, modern retry, attempt 1) (on server-00-01-ismg4.mongodb.net:27017, modern retry, attempt 1)"
"2020-02-06T04:51:42.387+09:00" Mongo::Error::SocketError (Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (server-00-01-ismg4.mongodb.net:27017, TLS)) (on server-00-01-ismg4.mongodb.net:27017, modern retry, attempt 2) (on server-00-01-ismg4.mongodb.net:27017, modern retry, attempt 2)):"

mongo-ruby-driver: 2.11.2
mongoid: 6.4.4
MongoDB Atlas: 3.6

What is the exception and stack trace produced by the driver?

Thank you @Oleg_Pudeyev.
I think it would wait for server_selection_timeout if it failed the first time, but it doesn’t.

Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017
  from mongo/socket/ssl.rb:49:in `connect'
  from mongo/socket/ssl.rb:49:in `block (2 levels) in connect!'
  from mongo/socket.rb:315:in `handle_errors'
  from mongo/socket/ssl.rb:49:in `block in connect!'
  from timeout.rb:93:in `block in timeout'
  from timeout.rb:103:in `timeout'
  from mongo/socket/ssl.rb:48:in `connect!'
  from mongo/socket/ssl.rb:81:in `initialize'
  from mongo/address/ipv4.rb:91:in `new'
  from mongo/address/ipv4.rb:91:in `socket'
  from mongo/address.rb:205:in `block in create_resolver'
  from mongo/address.rb:202:in `each'
  from mongo/address.rb:202:in `create_resolver'
  from mongo/address.rb:162:in `socket'
  from mongo/server/connection.rb:190:in `do_connect'
  from mongo/server/connection.rb:177:in `connect!'
  from mongo/server/connection_pool.rb:731:in `connect_connection'
  from mongo/server/connection_pool.rb:357:in `check_out'
  from mongo/server/connection_pool.rb:556:in `with_connection'
  from mongo/server.rb:417:in `with_connection'
  from mongo/operation/shared/executable.rb:55:in `dispatch_message'
  from mongo/operation/shared/executable.rb:50:in `get_result'
  from mongo/operation/shared/executable.rb:29:in `block (3 levels) in do_execute'
  from mongo/operation/shared/response_handling.rb:87:in `add_server_diagnostics'
  from mongo/operation/shared/executable.rb:28:in `block (2 levels) in do_execute'
  from mongo/operation/shared/response_handling.rb:43:in `add_error_labels'
  from mongo/operation/shared/executable.rb:27:in `block in do_execute'
  from mongo/operation/shared/response_handling.rb:73:in `unpin_maybe'
  from mongo/operation/shared/executable.rb:26:in `do_execute'
  from mongo/operation/shared/executable.rb:38:in `execute'
  from mongo/operation/shared/op_msg_or_find_command.rb:28:in `execute'
  from mongo/collection/view/iterable.rb:89:in `send_initial_query'
  from mongo/collection/view/iterable.rb:46:in `block in each'
  from mongo/retryable.rb:61:in `block in read_with_retry_cursor'
  from mongo/retryable.rb:316:in `modern_read_with_retry'
  from mongo/retryable.rb:117:in `read_with_retry'
  from mongo/retryable.rb:60:in `read_with_retry_cursor'
  from mongo/collection/view/iterable.rb:45:in `each'
  from mongoid/query_cache.rb:222:in `each'
  from mongoid/contextual/mongo.rb:258:in `first'
  from mongoid/contextual/mongo.rb:258:in `block in first'
  from mongoid/contextual/mongo.rb:531:in `try_cache'
  from mongoid/contextual/mongo.rb:256:in `first'
  from mongoid/contextual.rb:20:in `first'
  from mongoid/relations/builders/referenced/one.rb:20:in `build'
  from mongoid/relations/accessors.rb:42:in `create_relation'
  from mongoid/relations/accessors.rb:25:in `__build__'
  from mongoid/relations/accessors.rb:103:in `block (2 levels) in get_relation'
  from mongoid/threaded/lifecycle.rb:130:in `_loading'
  from mongoid/relations/accessors.rb:99:in `block in get_relation'
  from mongoid/threaded/lifecycle.rb:89:in `_building'
  from mongoid/relations/accessors.rb:98:in `get_relation'
  from mongoid/relations/accessors.rb:186:in `block in getter'
  from app/models/store.rb:1208:in `ship_setting_statuses'
  from mongoid/relations/proxy.rb:121:in `method_missing'
  from app/decorators/store_decorator.rb:334:in `meta_data'
  from app/views/layouts/store.html.erb:58:in `_app_views_layouts_store_html_erb___1904580886405372244_152195780'
  from action_view/template.rb:159:in `block in render'
  from active_support/notifications.rb:170:in `instrument'
  from action_view/template.rb:354:in `instrument_render_template'
  from action_view/template.rb:157:in `render'
  from action_view/renderer/template_renderer.rb:66:in `render_with_layout'
  from action_view/renderer/template_renderer.rb:52:in `render_template'
  from action_view/renderer/template_renderer.rb:16:in `render'
  from action_view/renderer/renderer.rb:44:in `render_template'
  from action_view/renderer/renderer.rb:25:in `render'
  from action_view/rendering.rb:103:in `_render_template'
  from action_controller/metal/streaming.rb:219:in `_render_template'
  from action_view/rendering.rb:84:in `render_to_body'
  from action_controller/metal/rendering.rb:52:in `render_to_body'
  from action_controller/metal/renderers.rb:142:in `render_to_body'
  from abstract_controller/rendering.rb:25:in `render'
  from action_controller/metal/rendering.rb:36:in `render'
  from action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
  from active_support/core_ext/benchmark.rb:14:in `block in ms'
  from benchmark.rb:308:in `realtime'
  from active_support/core_ext/benchmark.rb:14:in `ms'
  from action_controller/metal/instrumentation.rb:46:in `block in render'
  from action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
  from active_record/railties/controller_runtime.rb:36:in `cleanup_view_runtime'
  from mongoid/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  from action_controller/metal/instrumentation.rb:45:in `render'
  from wicked_pdf/pdf_helper.rb:46:in `call'
  from wicked_pdf/pdf_helper.rb:46:in `render_with_wicked_pdf'
  from wicked_pdf/pdf_helper.rb:30:in `render'
  from app/controllers/application_controller.rb:288:in `store_render'
  from app/controllers/news_controller.rb:32:in `index_page'
  from action_controller/metal/basic_implicit_render.rb:6:in `send_action'
  from abstract_controller/base.rb:194:in `process_action'
  from ddtrace/contrib/action_pack/action_controller/instrumentation.rb:114:in `process_action'
  from action_controller/metal/rendering.rb:30:in `process_action'
  from abstract_controller/callbacks.rb:42:in `block in process_action'
  from active_support/callbacks.rb:109:in `block in run_callbacks'
  from raven/integrations/rails/controller_transaction.rb:7:in `block in included'
  from active_support/callbacks.rb:118:in `instance_exec'
  from active_support/callbacks.rb:118:in `block in run_callbacks'
  from active_support/callbacks.rb:136:in `run_callbacks'
  from abstract_controller/callbacks.rb:41:in `process_action'
  from action_controller/metal/rescue.rb:22:in `process_action'
  from action_controller/metal/instrumentation.rb:34:in `block in process_action'
  from active_support/notifications.rb:168:in `block in instrument'
  from active_support/notifications/instrumenter.rb:23:in `instrument'
  from active_support/notifications.rb:168:in `instrument'
  from action_controller/metal/instrumentation.rb:32:in `process_action'
  from action_controller/metal/params_wrapper.rb:256:in `process_action'
  from active_record/railties/controller_runtime.rb:24:in `process_action'
  from mongoid/railties/controller_runtime.rb:19:in `process_action'
  from abstract_controller/base.rb:134:in `process'
  from action_view/rendering.rb:32:in `process'
  from action_controller/metal.rb:191:in `dispatch'
  from action_controller/metal.rb:252:in `dispatch'
  from action_dispatch/routing/route_set.rb:52:in `dispatch'
  from action_dispatch/routing/route_set.rb:34:in `serve'
  from action_dispatch/routing/mapper.rb:18:in `block in <class:Constraints>'
  from action_dispatch/routing/mapper.rb:48:in `serve'
  from action_dispatch/journey/router.rb:52:in `block in serve'
  from action_dispatch/journey/router.rb:35:in `each'
  from action_dispatch/journey/router.rb:35:in `serve'
  from action_dispatch/routing/route_set.rb:840:in `call'
  from warden/manager.rb:36:in `block in call'
  from warden/manager.rb:35:in `catch'
  from warden/manager.rb:35:in `call'
  from omniauth/strategy.rb:192:in `call!'
  from omniauth/strategy.rb:169:in `call'
  from omniauth/strategy.rb:192:in `call!'
  from omniauth/strategy.rb:169:in `call'
  from omniauth/builder.rb:64:in `call'
  from config/initializers/session_store.rb:37:in `call'
  from rack/attack.rb:182:in `call'
  from rack/tempfile_reaper.rb:15:in `call'
  from rack/etag.rb:25:in `call'
  from rack/conditional_get.rb:25:in `call'
  from rack/head.rb:12:in `call'
  from action_dispatch/http/content_security_policy.rb:18:in `call'
  from rack/session/abstract/id.rb:259:in `context'
  from rack/session/abstract/id.rb:253:in `call'
  from action_dispatch/middleware/cookies.rb:670:in `call'
  from action_dispatch/middleware/callbacks.rb:28:in `block in call'
  from active_support/callbacks.rb:98:in `run_callbacks'
  from action_dispatch/middleware/callbacks.rb:26:in `call'
  from action_dispatch/middleware/debug_exceptions.rb:61:in `call'
  from ddtrace/contrib/rails/middlewares.rb:17:in `call'
  from action_dispatch/middleware/show_exceptions.rb:33:in `call'
  from lograge/rails_ext/rack/logger.rb:15:in `call_app'
  from rails/rack/logger.rb:26:in `block in call'
  from active_support/tagged_logging.rb:71:in `block in tagged'
  from active_support/tagged_logging.rb:28:in `tagged'
  from active_support/tagged_logging.rb:71:in `tagged'
  from rails/rack/logger.rb:26:in `call'
  from action_dispatch/middleware/remote_ip.rb:81:in `call'
  from request_store/middleware.rb:19:in `call'
  from action_dispatch/middleware/request_id.rb:27:in `call'
  from rack/method_override.rb:22:in `call'
  from rack/runtime.rb:22:in `call'
  from action_dispatch/middleware/executor.rb:14:in `call'
  from action_dispatch/middleware/static.rb:127:in `call'
  from rack/sendfile.rb:111:in `call'
  from rack/utf8_sanitizer.rb:22:in `call'
  from raven/integrations/rack.rb:51:in `call'
  from ddtrace/contrib/rack/middlewares.rb:85:in `call'
  from rails/engine.rb:524:in `call'
  from unicorn/http_server.rb:576:in `process_client'
  from unicorn/worker_killer.rb:52:in `process_client'
  from unicorn/http_server.rb:670:in `worker_loop'
  from unicorn/http_server.rb:525:in `spawn_missing_workers'
  from unicorn/http_server.rb:536:in `maintain_worker_count'
  from unicorn/http_server.rb:294:in `join'
  from /var/www/xxx/shared/bundle/ruby/2.5.0/gems/unicorn-4.8.3/bin/unicorn_rails:209:in `<top (required)>'
  from /var/www/xxx/shared/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `load'
  from /var/www/xxx/shared/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `<main>'
Mongo::Error::SocketError: Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (server-shard-00-02-ismg4.mongodb.net:27017, TLS)) (on server-shard-00-02-ismg4.mongodb.net:27017, modern retry, attempt 1)
  from mongo/socket.rb:319:in `rescue in handle_errors'
  from mongo/socket.rb:313:in `handle_errors'
  from mongo/socket/ssl.rb:49:in `block in connect!'
  from timeout.rb:93:in `block in timeout'
  from timeout.rb:103:in `timeout'
  from mongo/socket/ssl.rb:48:in `connect!'
  from mongo/socket/ssl.rb:81:in `initialize'
  from mongo/address/ipv4.rb:91:in `new'
  from mongo/address/ipv4.rb:91:in `socket'
  from mongo/address.rb:205:in `block in create_resolver'
  from mongo/address.rb:202:in `each'
  from mongo/address.rb:202:in `create_resolver'
  from mongo/address.rb:162:in `socket'
  from mongo/server/connection.rb:190:in `do_connect'
  from mongo/server/connection.rb:177:in `connect!'
  from mongo/server/connection_pool.rb:731:in `connect_connection'
  from mongo/server/connection_pool.rb:357:in `check_out'
  from mongo/server/connection_pool.rb:556:in `with_connection'
  from mongo/server.rb:417:in `with_connection'
  from mongo/operation/shared/executable.rb:55:in `dispatch_message'
  from mongo/operation/shared/executable.rb:50:in `get_result'
  from mongo/operation/shared/executable.rb:29:in `block (3 levels) in do_execute'
  from mongo/operation/shared/response_handling.rb:87:in `add_server_diagnostics'
  from mongo/operation/shared/executable.rb:28:in `block (2 levels) in do_execute'
  from mongo/operation/shared/response_handling.rb:43:in `add_error_labels'
  from mongo/operation/shared/executable.rb:27:in `block in do_execute'
  from mongo/operation/shared/response_handling.rb:73:in `unpin_maybe'
  from mongo/operation/shared/executable.rb:26:in `do_execute'
  from mongo/operation/shared/executable.rb:38:in `execute'
  from mongo/operation/shared/op_msg_or_find_command.rb:28:in `execute'
  from mongo/collection/view/iterable.rb:89:in `send_initial_query'
  from mongo/collection/view/iterable.rb:46:in `block in each'
  from mongo/retryable.rb:61:in `block in read_with_retry_cursor'
  from mongo/retryable.rb:316:in `modern_read_with_retry'
  from mongo/retryable.rb:117:in `read_with_retry'
  from mongo/retryable.rb:60:in `read_with_retry_cursor'
  from mongo/collection/view/iterable.rb:45:in `each'
  from mongoid/query_cache.rb:222:in `each'
  from mongoid/contextual/mongo.rb:258:in `first'
  from mongoid/contextual/mongo.rb:258:in `block in first'
  from mongoid/contextual/mongo.rb:531:in `try_cache'
  from mongoid/contextual/mongo.rb:256:in `first'
  from mongoid/contextual.rb:20:in `first'
  from mongoid/relations/builders/referenced/one.rb:20:in `build'
  from mongoid/relations/accessors.rb:42:in `create_relation'
  from mongoid/relations/accessors.rb:25:in `__build__'
  from mongoid/relations/accessors.rb:103:in `block (2 levels) in get_relation'
  from mongoid/threaded/lifecycle.rb:130:in `_loading'
  from mongoid/relations/accessors.rb:99:in `block in get_relation'
  from mongoid/threaded/lifecycle.rb:89:in `_building'
  from mongoid/relations/accessors.rb:98:in `get_relation'
  from mongoid/relations/accessors.rb:186:in `block in getter'
  from app/models/store.rb:1208:in `ship_setting_statuses'
  from mongoid/relations/proxy.rb:121:in `method_missing'
  from app/decorators/store_decorator.rb:334:in `meta_data'
  from app/views/layouts/store.html.erb:58:in `_app_views_layouts_store_html_erb___1904580886405372244_152195780'
  from action_view/template.rb:159:in `block in render'
  from active_support/notifications.rb:170:in `instrument'
  from action_view/template.rb:354:in `instrument_render_template'
  from action_view/template.rb:157:in `render'
  from action_view/renderer/template_renderer.rb:66:in `render_with_layout'
  from action_view/renderer/template_renderer.rb:52:in `render_template'
  from action_view/renderer/template_renderer.rb:16:in `render'
  from action_view/renderer/renderer.rb:44:in `render_template'
  from action_view/renderer/renderer.rb:25:in `render'
  from action_view/rendering.rb:103:in `_render_template'
  from action_controller/metal/streaming.rb:219:in `_render_template'
  from action_view/rendering.rb:84:in `render_to_body'
  from action_controller/metal/rendering.rb:52:in `render_to_body'
  from action_controller/metal/renderers.rb:142:in `render_to_body'
  from abstract_controller/rendering.rb:25:in `render'
  from action_controller/metal/rendering.rb:36:in `render'
  from action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
  from active_support/core_ext/benchmark.rb:14:in `block in ms'
  from benchmark.rb:308:in `realtime'
  from active_support/core_ext/benchmark.rb:14:in `ms'
  from action_controller/metal/instrumentation.rb:46:in `block in render'
  from action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
  from active_record/railties/controller_runtime.rb:36:in `cleanup_view_runtime'
  from mongoid/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  from action_controller/metal/instrumentation.rb:45:in `render'
  from wicked_pdf/pdf_helper.rb:46:in `call'
  from wicked_pdf/pdf_helper.rb:46:in `render_with_wicked_pdf'
  from wicked_pdf/pdf_helper.rb:30:in `render'
  from app/controllers/application_controller.rb:288:in `store_render'
  from app/controllers/news_controller.rb:32:in `index_page'
  from action_controller/metal/basic_implicit_render.rb:6:in `send_action'
  from abstract_controller/base.rb:194:in `process_action'
  from ddtrace/contrib/action_pack/action_controller/instrumentation.rb:114:in `process_action'
  from action_controller/metal/rendering.rb:30:in `process_action'
  from abstract_controller/callbacks.rb:42:in `block in process_action'
  from active_support/callbacks.rb:109:in `block in run_callbacks'
  from raven/integrations/rails/controller_transaction.rb:7:in `block in included'
  from active_support/callbacks.rb:118:in `instance_exec'
  from active_support/callbacks.rb:118:in `block in run_callbacks'
  from active_support/callbacks.rb:136:in `run_callbacks'
  from abstract_controller/callbacks.rb:41:in `process_action'
  from action_controller/metal/rescue.rb:22:in `process_action'
  from action_controller/metal/instrumentation.rb:34:in `block in process_action'
  from active_support/notifications.rb:168:in `block in instrument'
  from active_support/notifications/instrumenter.rb:23:in `instrument'
  from active_support/notifications.rb:168:in `instrument'
  from action_controller/metal/instrumentation.rb:32:in `process_action'
  from action_controller/metal/params_wrapper.rb:256:in `process_action'
  from active_record/railties/controller_runtime.rb:24:in `process_action'
  from mongoid/railties/controller_runtime.rb:19:in `process_action'
  from abstract_controller/base.rb:134:in `process'
  from action_view/rendering.rb:32:in `process'
  from action_controller/metal.rb:191:in `dispatch'
  from action_controller/metal.rb:252:in `dispatch'
  from action_dispatch/routing/route_set.rb:52:in `dispatch'
  from action_dispatch/routing/route_set.rb:34:in `serve'
  from action_dispatch/routing/mapper.rb:18:in `block in <class:Constraints>'
  from action_dispatch/routing/mapper.rb:48:in `serve'
  from action_dispatch/journey/router.rb:52:in `block in serve'
  from action_dispatch/journey/router.rb:35:in `each'
  from action_dispatch/journey/router.rb:35:in `serve'
  from action_dispatch/routing/route_set.rb:840:in `call'
  from warden/manager.rb:36:in `block in call'
  from warden/manager.rb:35:in `catch'
  from warden/manager.rb:35:in `call'
  from omniauth/strategy.rb:192:in `call!'
  from omniauth/strategy.rb:169:in `call'
  from omniauth/strategy.rb:192:in `call!'
  from omniauth/strategy.rb:169:in `call'
  from omniauth/builder.rb:64:in `call'
  from config/initializers/session_store.rb:37:in `call'
  from rack/attack.rb:182:in `call'
  from rack/tempfile_reaper.rb:15:in `call'
  from rack/etag.rb:25:in `call'
  from rack/conditional_get.rb:25:in `call'
  from rack/head.rb:12:in `call'
  from action_dispatch/http/content_security_policy.rb:18:in `call'
  from rack/session/abstract/id.rb:259:in `context'
  from rack/session/abstract/id.rb:253:in `call'
  from action_dispatch/middleware/cookies.rb:670:in `call'
  from action_dispatch/middleware/callbacks.rb:28:in `block in call'
  from active_support/callbacks.rb:98:in `run_callbacks'
  from action_dispatch/middleware/callbacks.rb:26:in `call'
  from action_dispatch/middleware/debug_exceptions.rb:61:in `call'
  from ddtrace/contrib/rails/middlewares.rb:17:in `call'
  from action_dispatch/middleware/show_exceptions.rb:33:in `call'
  from lograge/rails_ext/rack/logger.rb:15:in `call_app'
  from rails/rack/logger.rb:26:in `block in call'
  from active_support/tagged_logging.rb:71:in `block in tagged'
  from active_support/tagged_logging.rb:28:in `tagged'
  from active_support/tagged_logging.rb:71:in `tagged'
  from rails/rack/logger.rb:26:in `call'
  from action_dispatch/middleware/remote_ip.rb:81:in `call'
  from request_store/middleware.rb:19:in `call'
  from action_dispatch/middleware/request_id.rb:27:in `call'
  from rack/method_override.rb:22:in `call'
  from rack/runtime.rb:22:in `call'
  from action_dispatch/middleware/executor.rb:14:in `call'
  from action_dispatch/middleware/static.rb:127:in `call'
  from rack/sendfile.rb:111:in `call'
  from rack/utf8_sanitizer.rb:22:in `call'
  from raven/integrations/rack.rb:51:in `call'
  from ddtrace/contrib/rack/middlewares.rb:85:in `call'
  from rails/engine.rb:524:in `call'
  from unicorn/http_server.rb:576:in `process_client'
  from unicorn/worker_killer.rb:52:in `process_client'
  from unicorn/http_server.rb:670:in `worker_loop'
  from unicorn/http_server.rb:525:in `spawn_missing_workers'
  from unicorn/http_server.rb:536:in `maintain_worker_count'
  from unicorn/http_server.rb:294:in `join'
  from /var/www/xxx/shared/bundle/ruby/2.5.0/gems/unicorn-4.8.3/bin/unicorn_rails:209:in `<top (required)>'
  from /var/www/xxx/shared/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `load'
  from /var/www/xxx/shared/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `<main>'
Mongo::Error::OperationFailure: interrupted at shutdown (11600) (on server-shard-00-01-ismg4.mongodb.net:27017, modern retry, attempt 2)
  from mongo/operation/result.rb:297:in `raise_operation_failure'
  from mongo/operation/result.rb:268:in `validate!'
  from mongo/operation/shared/response_handling.rb:29:in `block (3 levels) in validate_result'
  from mongo/operation/shared/response_handling.rb:87:in `add_server_diagnostics'
  from mongo/operation/shared/response_handling.rb:28:in `block (2 levels) in validate_result'
  from mongo/operation/shared/response_handling.rb:43:in `add_error_labels'
  from mongo/operation/shared/response_handling.rb:27:in `block in validate_result'
  from mongo/operation/shared/response_handling.rb:73:in `unpin_maybe'
  from mongo/operation/shared/response_handling.rb:26:in `validate_result'
  from mongo/operation/shared/executable.rb:39:in `block in execute'
  from mongo/operation/shared/executable.rb:38:in `tap'
  from mongo/operation/shared/executable.rb:38:in `execute'
  from mongo/operation/shared/op_msg_or_find_command.rb:28:in `execute'
  from mongo/collection/view/iterable.rb:89:in `send_initial_query'
  from mongo/collection/view/iterable.rb:46:in `block in each'
  from mongo/retryable.rb:61:in `block in read_with_retry_cursor'
  from mongo/retryable.rb:392:in `retry_read'
  from mongo/retryable.rb:323:in `rescue in modern_read_with_retry'
  from mongo/retryable.rb:315:in `modern_read_with_retry'
  from mongo/retryable.rb:117:in `read_with_retry'
  from mongo/retryable.rb:60:in `read_with_retry_cursor'
  from mongo/collection/view/iterable.rb:45:in `each'
  from mongoid/query_cache.rb:222:in `each'
  from mongoid/contextual/mongo.rb:258:in `first'
  from mongoid/contextual/mongo.rb:258:in `block in first'
  from mongoid/contextual/mongo.rb:531:in `try_cache'
  from mongoid/contextual/mongo.rb:256:in `first'
  from mongoid/contextual.rb:20:in `first'
  from mongoid/relations/builders/referenced/one.rb:20:in `build'
  from mongoid/relations/accessors.rb:42:in `create_relation'
  from mongoid/relations/accessors.rb:25:in `__build__'
  from mongoid/relations/accessors.rb:103:in `block (2 levels) in get_relation'
  from mongoid/threaded/lifecycle.rb:130:in `_loading'
  from mongoid/relations/accessors.rb:99:in `block in get_relation'
  from mongoid/threaded/lifecycle.rb:89:in `_building'
  from mongoid/relations/accessors.rb:98:in `get_relation'
  from mongoid/relations/accessors.rb:186:in `block in getter'
  from app/models/store.rb:1208:in `ship_setting_statuses'
  from mongoid/relations/proxy.rb:121:in `method_missing'
  from app/decorators/store_decorator.rb:334:in `meta_data'
  from app/views/layouts/store.html.erb:58:in `_app_views_layouts_store_html_erb___1904580886405372244_152195780'
  from action_view/template.rb:159:in `block in render'
  from active_support/notifications.rb:170:in `instrument'
  from action_view/template.rb:354:in `instrument_render_template'
  from action_view/template.rb:157:in `render'
  from action_view/renderer/template_renderer.rb:66:in `render_with_layout'
  from action_view/renderer/template_renderer.rb:52:in `render_template'
  from action_view/renderer/template_renderer.rb:16:in `render'
  from action_view/renderer/renderer.rb:44:in `render_template'
  from action_view/renderer/renderer.rb:25:in `render'
  from action_view/rendering.rb:103:in `_render_template'
  from action_controller/metal/streaming.rb:219:in `_render_template'
  from action_view/rendering.rb:84:in `render_to_body'
  from action_controller/metal/rendering.rb:52:in `render_to_body'
  from action_controller/metal/renderers.rb:142:in `render_to_body'
  from abstract_controller/rendering.rb:25:in `render'
  from action_controller/metal/rendering.rb:36:in `render'
  from action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
  from active_support/core_ext/benchmark.rb:14:in `block in ms'
  from benchmark.rb:308:in `realtime'
  from active_support/core_ext/benchmark.rb:14:in `ms'
  from action_controller/metal/instrumentation.rb:46:in `block in render'
  from action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
  from active_record/railties/controller_runtime.rb:36:in `cleanup_view_runtime'
  from mongoid/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  from action_controller/metal/instrumentation.rb:45:in `render'
  from wicked_pdf/pdf_helper.rb:46:in `call'
  from wicked_pdf/pdf_helper.rb:46:in `render_with_wicked_pdf'
  from wicked_pdf/pdf_helper.rb:30:in `render'
  from app/controllers/application_controller.rb:288:in `store_render'
  from app/controllers/news_controller.rb:32:in `index_page'
  from action_controller/metal/basic_implicit_render.rb:6:in `send_action'
  from abstract_controller/base.rb:194:in `process_action'
  from ddtrace/contrib/action_pack/action_controller/instrumentation.rb:114:in `process_action'
  from action_controller/metal/rendering.rb:30:in `process_action'
  from abstract_controller/callbacks.rb:42:in `block in process_action'
  from active_support/callbacks.rb:109:in `block in run_callbacks'
  from raven/integrations/rails/controller_transaction.rb:7:in `block in included'
  from active_support/callbacks.rb:118:in `instance_exec'
  from active_support/callbacks.rb:118:in `block in run_callbacks'
  from active_support/callbacks.rb:136:in `run_callbacks'
  from abstract_controller/callbacks.rb:41:in `process_action'
  from action_controller/metal/rescue.rb:22:in `process_action'
  from action_controller/metal/instrumentation.rb:34:in `block in process_action'
  from active_support/notifications.rb:168:in `block in instrument'
  from active_support/notifications/instrumenter.rb:23:in `instrument'
  from active_support/notifications.rb:168:in `instrument'
  from action_controller/metal/instrumentation.rb:32:in `process_action'
  from action_controller/metal/params_wrapper.rb:256:in `process_action'
  from active_record/railties/controller_runtime.rb:24:in `process_action'
  from mongoid/railties/controller_runtime.rb:19:in `process_action'
  from abstract_controller/base.rb:134:in `process'
  from action_view/rendering.rb:32:in `process'
  from action_controller/metal.rb:191:in `dispatch'
  from action_controller/metal.rb:252:in `dispatch'
  from action_dispatch/routing/route_set.rb:52:in `dispatch'
  from action_dispatch/routing/route_set.rb:34:in `serve'
  from action_dispatch/routing/mapper.rb:18:in `block in <class:Constraints>'
  from action_dispatch/routing/mapper.rb:48:in `serve'
  from action_dispatch/journey/router.rb:52:in `block in serve'
  from action_dispatch/journey/router.rb:35:in `each'
  from action_dispatch/journey/router.rb:35:in `serve'
  from action_dispatch/routing/route_set.rb:840:in `call'
  from warden/manager.rb:36:in `block in call'
  from warden/manager.rb:35:in `catch'
  from warden/manager.rb:35:in `call'
  from omniauth/strategy.rb:192:in `call!'
  from omniauth/strategy.rb:169:in `call'
  from omniauth/strategy.rb:192:in `call!'
  from omniauth/strategy.rb:169:in `call'
  from omniauth/builder.rb:64:in `call'
  from config/initializers/session_store.rb:37:in `call'
  from rack/attack.rb:182:in `call'
  from rack/tempfile_reaper.rb:15:in `call'
  from rack/etag.rb:25:in `call'
  from rack/conditional_get.rb:25:in `call'
  from rack/head.rb:12:in `call'
  from action_dispatch/http/content_security_policy.rb:18:in `call'
  from rack/session/abstract/id.rb:259:in `context'
  from rack/session/abstract/id.rb:253:in `call'
  from action_dispatch/middleware/cookies.rb:670:in `call'
  from action_dispatch/middleware/callbacks.rb:28:in `block in call'
  from active_support/callbacks.rb:98:in `run_callbacks'
  from action_dispatch/middleware/callbacks.rb:26:in `call'
  from action_dispatch/middleware/debug_exceptions.rb:61:in `call'
  from ddtrace/contrib/rails/middlewares.rb:17:in `call'
  from action_dispatch/middleware/show_exceptions.rb:33:in `call'
  from lograge/rails_ext/rack/logger.rb:15:in `call_app'
  from rails/rack/logger.rb:26:in `block in call'
  from active_support/tagged_logging.rb:71:in `block in tagged'
  from active_support/tagged_logging.rb:28:in `tagged'
  from active_support/tagged_logging.rb:71:in `tagged'
  from rails/rack/logger.rb:26:in `call'
  from action_dispatch/middleware/remote_ip.rb:81:in `call'
  from request_store/middleware.rb:19:in `call'
  from action_dispatch/middleware/request_id.rb:27:in `call'
  from rack/method_override.rb:22:in `call'
  from rack/runtime.rb:22:in `call'
  from action_dispatch/middleware/executor.rb:14:in `call'
  from action_dispatch/middleware/static.rb:127:in `call'
  from rack/sendfile.rb:111:in `call'
  from rack/utf8_sanitizer.rb:22:in `call'
  from raven/integrations/rack.rb:51:in `call'
  from ddtrace/contrib/rack/middlewares.rb:85:in `call'
  from rails/engine.rb:524:in `call'
  from unicorn/http_server.rb:576:in `process_client'
  from unicorn/worker_killer.rb:52:in `process_client'
  from unicorn/http_server.rb:670:in `worker_loop'
  from unicorn/http_server.rb:525:in `spawn_missing_workers'
  from unicorn/http_server.rb:536:in `maintain_worker_count'
  from unicorn/http_server.rb:294:in `join'
  from /var/www/xxx/shared/bundle/ruby/2.5.0/gems/unicorn-4.8.3/bin/unicorn_rails:209:in `<top (required)>'
  from /var/www/xxx/shared/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `load'
  from /var/www/xxx/shared/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `<main>'

As you’re using Mongoid with Unicorn can you verify that you’ve followed the Usage with Forking Servers guidance?

Thank you @alexbevi.

What you taught may be correct.
Maybe the parent connection remains.
I’ll give it a try.

I suspect this is a driver issue and am investigating.

@alexbevi @Oleg_Pudeyev

I set up and maintained according to the Usage with Forking Servers, but it doesn’t seem to wait for server_selection_timeout period to retry as before.

Is there anything else that could be the cause?

2020-03-19T05:08:05.194+09:00 [22269ef8-27d5-4e0a-8f2a-115c29d8a0a8] Read retry due to: Mongo::Error::SocketError Errno::ECONNRESET: Connection reset by peer - SSL_connect (for x.x.x.x:27017 (server-shard-00-03-ismg4.mongodb.net:27017, TLS)) (on server-shard-00-03-ismg4.mongodb.net:27017, modern retry, attempt 1) (on server-shard-00-03-ismg4.mongodb.net:27017, modern retry, attempt 1)
2020-03-19T05:08:05.208+09:00 MONGODB | Populator failed to connect a connection for server-shard-00-03-ismg4.mongodb.net:27017: Mongo::Error::SocketError: Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (server-shard-00-03-ismg4.mongodb.net:27017, TLS)).
2020-03-19T05:08:05.587+09:00 [22269ef8-27d5-4e0a-8f2a-115c29d8a0a8] Read retry due to: Mongo::Error::SocketError Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (server-shard-00-03-ismg4.mongodb.net:27017, TLS)) (on server-shard-00-03-ismg4.mongodb.net:27017, modern retry, attempt 1) (on server-shard-00-03-ismg4.mongodb.net:27017, modern retry, attempt 1)
2020-03-19T05:08:05.608+09:00 MONGODB | Populator failed to connect a connection for server-shard-00-03-ismg4.mongodb.net:27017: Mongo::Error::SocketError: Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (server-shard-00-03-ismg4.mongodb.net:27017, TLS)).
2020-03-19T05:08:05.803+09:00 [22269ef8-27d5-4e0a-8f2a-115c29d8a0a8] Read retry due to: Mongo::Error::SocketError Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (prod-mongodb-shard-00-03-ismg4.mongodb.net:27017, TLS)) (on server-shard-00-03-ismg4.mongodb.net:27017, modern retry, attempt 1) (on server-shard-00-03-ismg4.mongodb.net:27017, modern retry, attempt 1)
2020-03-19T05:08:05.809+09:00 MONGODB | Populator failed to connect a connection for server-shard-00-03-ismg4.mongodb.net:27017: Mongo::Error::SocketError: Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (server-shard-00-03-ismg4.mongodb.net:27017, TLS)). It will retry.
2020-03-19T05:08:05.825+09:00 MONGODB | Populator failed to connect a connection for server-shard-00-03-ismg4.mongodb.net:27017: Mongo::Error::SocketError: Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (server-shard-00-03-ismg4.mongodb.net:27017, TLS)). It will retry.
2020-03-19T05:08:05.971+09:00 [22269ef8-27d5-4e0a-8f2a-115c29d8a0a8] ActionView::Template::Error (Errno::ECONNREFUSED: Connection refused - connect(2) for x.x.x.x:27017 (for x.x.x.x:27017 (server-shard-00-03-ismg4.mongodb.net:27017, TLS)) (on server-shard-00-03-ismg4.mongodb.net:27017, modern retry, attempt 2) (on server-shard-00-03-ismg4.mongodb.net:27017, modern retry, attempt 2)):