railslove / rack-tracker

Tracking made easy: Don’t fool around with adding tracking and analytics partials to your app and concentrate on the things that matter.

Home Page:https://www.railslove.com/open-source

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RuntimeError: string modified

Artur-Sulej opened this issue · comments

response.sub! %r{</#{self.position}>} do |m|

Method sub! casuses RuntimeError string modified.
Ruby: 2.4.2
Rails: 5.1.4

hi, thanks for your report. do you have the full exception trace for the error?

RuntimeError: string modified
  from rack/tracker/handler.rb:41:in `sub!'
  from rack/tracker/handler.rb:41:in `inject'
  from rack/tracker.rb:62:in `block in inject'
  from rack/tracker.rb:95:in `each'
  from rack/tracker.rb:95:in `each'
  from rack/tracker.rb:61:in `inject'
  from rack/tracker.rb:50:in `block in call'
  from rack/body_proxy.rb:36:in `block in each'
  from rack/response.rb:76:in `each'
  from rack/response.rb:76:in `each'
  from rack/body_proxy.rb:36:in `each'
  from rack/tracker.rb:50:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/cors.rb:97:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/attack.rb:147:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25: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 new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/etag.rb:25:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/conditional_get.rb:25:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/head.rb:12:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/session/abstract/id.rb:232:in `context'
  from rack/session/abstract/id.rb:226:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from action_dispatch/middleware/cookies.rb:613:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from action_dispatch/middleware/callbacks.rb:26:in `block in call'
  from active_support/callbacks.rb:97:in `run_callbacks'
  from action_dispatch/middleware/callbacks.rb:24:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from action_dispatch/middleware/debug_exceptions.rb:59:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from action_dispatch/middleware/show_exceptions.rb:31:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rails/rack/logger.rb:36:in `call_app'
  from rails/rack/logger.rb:24:in `block in call'
  from active_support/tagged_logging.rb:69:in `block in tagged'
  from active_support/tagged_logging.rb:26:in `tagged'
  from active_support/tagged_logging.rb:69:in `tagged'
  from rails/rack/logger.rb:24:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from action_dispatch/middleware/remote_ip.rb:79:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from request_store/middleware.rb:9:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from action_dispatch/middleware/request_id.rb:25:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/method_override.rb:22:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/runtime.rb:22:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from action_dispatch/middleware/executor.rb:12:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from action_dispatch/middleware/static.rb:125:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/sendfile.rb:111:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rack/utf8_sanitizer.rb:20:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from raven/integrations/rack.rb:51:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from skylight/probes/middleware.rb:25:in `call'
  from rails/engine.rb:522:in `call'
  from new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  from puma/configuration.rb:225:in `call'
  from puma/server.rb:605:in `handle_request'
  from puma/server.rb:437:in `process_client'
  from puma/server.rb:301:in `block in run'
  from puma/thread_pool.rb:120:in `block in spawn_thread'
commented

Hm... does this happen on your local machine?
While bundling or when you startup the server?
Are you running your code intentionally with RUBYOPT=--enable-frozen-string-literal?

I believe most Ruby code still breaks when you turn on the frozen string literals feature.
When disabling it is not an option (--disable-frozen-string-literal),
we need to port the places where we mutate strings (sub! for example...).
But I have no estimate how that will impact performance etc...

Nonetheless, it's definitely something we have to fix sometime, since it will become the default in Ruby 3.0 when I'm informed correctly.

The confusing thing is, when I run the test suite with:

$ RUBYOPT=--enable-frozen-string-literal bundle exec rspec

Even bundler seems not to support this? 🤔 Or is the bug in my setup?

2.5.0/rubygems/bundler_version_finder.rb:105:in `untaint': can't modify frozen String (FrozenError)

Does it work for you, when you add # frozen_string_literal: false to the beginning of the handler.rb file? (You can edit the gems source with bundle open rack-tracker)
But I have no idea if that rules out the global setting? 🤔

Hi there @DonSchado! We're hitting this error in our application as we attempt to update to Rails 5.2 (which appears to have pushed forward the usage of frozen-string-literal all over actionpack. Would you accept a pull request to adjust the sub operation to not be done in-place (pardon my ignorance if there's more to be done here complexity-wise).

For context, the primary place where this occurs for us is when the Rack middleware is processing the response for a redirect and the Rack::Tracker middleware is being used. The response itself is frozen, which breaks when rack-tracker tries to jam in the desired tracker snippets. When that does happen, we see an exception that has a stacktrace like the one posted by @hot-cheese.

Happy to provide additional context as needed!

RUBY: 2.4.2
RAILS: 5.2

commented

Hi @jblock! :)
Thanks for reaching out, I thought we have more time until we face this again.
So a pull request that solves this issue would be very cool 👍
(btw the linked issue is 404 for me 🤔)

👍 Sounds good!

(btw the linked issue is 404 for me 🤔)

🤦‍♂️ My bad! Here it is: rails/rails#29933

Hi @DonSchado, I just put up a PR to resolve this.

Hello,
The problem seems not to be resolved.
I have exactly the same problem as @jblock
Any updates ?

It seems like the PR that fixes this issue was merged but a new gem was not generated. The version is still 1.6.0.

Installing the gem direct from github (instead of from rubygems) should fix it for you, for now.

commented

I'm very sorry... v1.7.0 is released now