Skip to content

fix(web): stop redefining the authorize method on every request - #552

Merged
artempartos merged 1 commit into
developfrom
artempartos/fix-web-memory-leak-authorize
Oct 24, 2026
Merged

artempartos merged 1 commit into
developfrom
artempartos/fix-web-memory-leak-authorize

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

Summary

Web processes grew with every page they served and were eventually OOMKilled. The cause is AuthorizationConcern#init_authorize: a prepend_before_action that, on every request, called

self.class.send(:define_method, policy_method_name) { send(:authorize, policy_method_params) }

The block is a closure over the controller handling that request. With YJIT on (the Rails default in production), superseded method definitions stay reachable from YJIT, so every request kept its controller — and through it the request, the response body and every record it loaded — alive for the life of the process. Heavy pages (the board's Inertia payload) made it grow fastest; controllers outside Web::Company/Api::V1 never ran the hook, which is why only web processes grew.

The generated authorize_<name> method existed only so dynamic_authorize! could send to it. dynamic_authorize! now calls authorize(policy_method_params) directly:

  • same Pundit call with the same namespaced policy arguments;
  • same filter position (before_action :dynamic_authorize! in both base controllers), and the existing skip_before_action :dynamic_authorize! call sites are untouched;
  • the removed ancestors.include?(ApplicationController) guard was always true — both classes that include the concern inherit from ::ApplicationController;
  • nothing called the generated authorize_* methods directly.

It also stops invalidating the controller's method caches and YJIT code on every request.

How it was found and verified

Reproduced on a local stand: the production image (v1.6.0) under real Puma, its own Postgres/Redis, a large seeded board, and a plain net/http client issuing the board's cable-triggered partial reloads and task-drawer visits (10 threads). A heap dump (ObjectSpace.dump_all) traced the retained response bodies to BoardsController instances held by a Proc reachable from AuthorizationConcern, rooted at YJIT.

Same 1,500-request load, Puma RSS:

Variant Start After 1,500 requests
v1.6.0, YJIT on (as deployed) 409 MiB 1,077 MiB, still growing (2.2 GiB after 4,000)
this change, YJIT on 354 MiB 482 MiB, flat after the first ~30 s
v1.6.0, YJIT off 357 MiB 457 MiB, flat

Sentry and the debug log level were ruled out on the same stand (growth unchanged with either off).

Tests

test/integration/web/company/authorization_defines_no_methods_test.rb drives a real company page and asserts the request defines nothing on its controller class (it fails on the old concern). Existing authorization and policy-coverage tests cover the unchanged authorization behaviour. Locally only a syntax check was run; CI is the verification for the suite, rubocop and brakeman.

Changelog: one untagged Fixed entry. It also moves the Docs entry from #551 out of [1.6.0] into [Unreleased]: #551 merged after the 1.6.0 release PR, so its entry landed in a release it is not part of.

🤖 Generated with Claude Code

AuthorizationConcern#init_authorize ran before every action and called
define_method on the controller class, with a block that closed over the
controller handling the request. With YJIT on, every superseded definition
stays reachable, so each request kept its controller, request, response
body and loaded records alive. Web processes grew with every page served
until they were OOMKilled.

The generated method only existed so dynamic_authorize! could send to it;
dynamic_authorize! now calls authorize with the same policy arguments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
@artempartos
artempartos merged commit 8611402 into develop Oct 24, 2026
16 checks passed
artempartos added a commit that referenced this pull request Oct 24, 2026
Request logs had an always-empty user field, and MCP calls logged nothing but
the path, so tracing a request to an account meant matching SQL binds by
request id. The web memory leak fixed in #552 grew for weeks before anyone
noticed; a periodic GC.stat/RSS line shows the next one as a trend within a
day and tells retained Ruby objects from native memory.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
artempartos added a commit that referenced this pull request Oct 24, 2026
Request logs had an always-empty user field, and MCP calls logged nothing but
the path, so tracing a request to an account meant matching SQL binds by
request id. The web memory leak fixed in #552 grew for weeks before anyone
noticed; a periodic GC.stat/RSS line shows the next one as a trend within a
day and tells retained Ruby objects from native memory.

Signed-off-by: Artem Petrov <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant