Skip to content

Conversation

@eileencodes
Copy link
Member

When using ActionController::Live the execution context is shared across threads. In some cases, like database context switching from a writer to a replica connection, you may not want that context to be shared.

This PR implements a config option for excluding specific keys from the shared execution. By default no keys are shared to support backwards compatibility in order to maintain current behavior while implementing a way for applications experiencing an issue with database connections to opt-out. See #52906.

To exclude a specific key application-wide, set the following config option:

config.action_controller.live_streaming_excluded_keys = [:active_record_connected_to_stack]

If you want finer-grained control over which controllers exclude these keys you can set live_streaming_excluded_keys in the controller itself.

class MyController < ApplicationController
  include ActionController::Live
  self.live_streaming_excluded_keys = [:active_record_connected_to_stack]

  def index
    ...
  end
end

In the future we may want to default to excluding this key since I'm not sure anyone wants to share database context. However I chose not to do that in this PR because we need to backport this to use as a bug fix, and if it changes behavior that could break applications.

Fixes #52906

When using `ActionController::Live` the execution context is shared
across threads. In some cases, like database context switching from a
writer to a replica connection, you may not want that context to be
shared.

This PR implements a config option for excluding specific keys from the
shared execution. By default no keys are shared to support backwards
compatibility in order to maintain current behavior while implementing a
way for applications experiencing an issue with database connections to
opt-out. See #52906.

To exclude a specific key application-wide, set the following config
option:

```ruby
config.action_controller.live_streaming_excluded_keys = [:active_record_connected_to_stack]
```

If you want finer-grained control over which controllers exclude these
keys you can set `live_streaming_excluded_keys` in the controller
itself.

```ruby
class MyController < ApplicationController
  include ActionController::Live
  self.live_streaming_excluded_keys = [:active_record_connected_to_stack]

  def index
    ...
  end
end
```

In the future we may want to default to excluding this key since I'm not
sure anyone wants to share database context. However I chose not to do
that in this PR because we need to backport this to use as a bug fix,
and if it changes behavior that could break applications.

Fixes #52906
@eileencodes eileencodes changed the title Imeplement exclude keys for ActionController::Live execution sharing Implement exclude keys for ActionController::Live execution sharing Dec 17, 2025
@eileencodes eileencodes merged commit 7570743 into main Dec 17, 2025
3 of 6 checks passed
@eileencodes eileencodes deleted the add-exclude-keys-to-live-controller branch December 17, 2025 20:21
eileencodes added a commit that referenced this pull request Dec 17, 2025
Implement exclude keys for ActionController::Live execution sharing
eileencodes added a commit that referenced this pull request Dec 17, 2025
Implement exclude keys for ActionController::Live execution sharing
# shares execution state from the parent thread.
#
# You can configure which execution state keys should be excluded from being shared
# using the `config.action_controller.live.streaming_excluded_keys` configuration:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be

Suggested change
# using the `config.action_controller.live.streaming_excluded_keys` configuration:
# using the `config.action_controller.live_streaming_excluded_keys` configuration:

?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ActionController::Live and ActiveRecord::Base.connected_to stale db state

3 participants