Skip to content

Guard Proxy#stop against already-stopped EventMachine reactor - #359

Merged
ronwsmith merged 2 commits into
oesmith:masterfrom
brightsidehealth:fix-proxy-stop-reactor-check
Mar 3, 2026
Merged

ronwsmith merged 2 commits into
oesmith:masterfrom
brightsidehealth:fix-proxy-stop-reactor-check

Conversation

@jeremyjbarth

@jeremyjbarth jeremyjbarth commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Adds EM.reactor_running? guard in Proxy#stop to prevent RuntimeError: eventmachine not initialized when the reactor has already stopped

Problem

When EventMachine has already stopped (e.g. during RSpec after(:suite) teardown), calling EM.get_sockname (via port) or EM.stop raises RuntimeError: eventmachine not initialized. This causes test suites to exit non-zero despite all tests passing ("1 error occurred outside of examples").

Solution

def stop
  return if @signature.nil?
  return unless EM.reactor_running?  # new guard

  server_port = port
  EM.stop
  wait_for_server_shutdown! server_port
end

The existing @signature.nil? check only guards against a proxy that was never started. It doesn't cover the case where EventMachine's reactor loop has already been shut down by another mechanism.

Fixes #253

Verification

  • bundle exec rspec spec/lib/proxy_spec.rb — all examples pass, 0 failures
  • Confirmed the fix resolves the eventmachine not initialized crash in a production Rails app with 935+ messaging specs using puffing-billy 4.0.0

When EventMachine has already stopped (e.g. in RSpec after(:suite)),
calling EM.get_sockname or EM.stop raises RuntimeError: eventmachine
not initialized. Add EM.reactor_running? check to prevent this.

Fixes oesmith#253
@jeremyjbarth jeremyjbarth reopened this Mar 2, 2026
@ronwsmith

Copy link
Copy Markdown
Collaborator

@jeremyjbarth thanks for the contribution! I don't have time to cut a new version right now but I will merge it so you can get off your fork. I'll report back with a new version soon.

@ronwsmith
ronwsmith merged commit 1b369a5 into oesmith:master Mar 3, 2026
5 checks passed
@ronwsmith

Copy link
Copy Markdown
Collaborator

@jeremyjbarth this has been released as 4.0.4: https://rubygems.org/gems/puffing-billy/versions/4.0.4

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

eventmachine not initialized - proxy isn't working

2 participants