I built a pretty small Rails 5.1.4 (Ruby 2.3.1) application. Once I've deployed it to production, I'm getting this particular error from time to time:
RuntimeError: can't add a new key into hash during iteration
Pointing here:
# rack/request.rb, line 67
def set_header(name, v)
@env[name] = v
end
I understand, this error happens when you're attempting to add new key to the hash while iterating over that hash. Since @env
is a hash, it makes sense. But:
- in a stacktrace I found nothing related to iterations over
@env
, it's a dead simple chain ofapp.call(env)
calls. - this error occurs not always, but just once per hour or two, so this is also super weird to me
- I can't reproduce it locally: I've tried to load server with multiple request hits, assuming this might be thread-safety issue, but locally it works like a charm...
Full stacktrace only consists of rack middlewares can be found here: https://gist.github.com/Nattfodd/e513122400b4115a653ea38d69917a9a
Gemfile.lock: https://gist.github.com/Nattfodd/a9015e9204544302bf3959cec466b715
Server is running with puma, config is very simple: just amount of threads and workers:
threads 0, 5
workers 5
My current ideas are:
- one of monitoring gems has a bug (sentry-raven, new_relic)
- concurrent-ruby has a bug (I read about one, but it was fixed in 1.0.2, and actual version I'm using for Puma is 1.0.5)
- something super stupid, I missed, but I have no idea where to look, since the controller's action contains 3 lines of code and application config is mostly default...
- this is something config-related, since backtrace does not contain the controller at all...