Skip to content

Remove with_level override when the block exits - #185

Closed
wtn wants to merge 1 commit into
ruby:masterfrom
wtn:level
Closed

wtn wants to merge 1 commit into
ruby:masterfrom
wtn:level

Conversation

@wtn

@wtn wtn commented Nov 16, 2026

Copy link
Copy Markdown

Fixes #135.

Logger#with_level saves the previous level by calling level, which falls back to @level when the current fiber has no override. The ensure clause then writes that value back as an override, and the entry for the calling fiber stays in @level_override:

logger = Logger.new(nil, level: :info)
logger.with_level(:debug) {}
logger.level = :error
logger.level # => 1

The leftover entry also keeps the fiber from being garbage collected while the logger is alive. A finished thread's root fiber is retained the same way, which is the leak reported in #135.

with_level now saves level_override[level_key] as the previous value, so the outermost call deletes its entry and a nested call restores the enclosing override.

with_level saved the effective level as the previous value, so the
ensure clause wrote it back as an override and left the entry for the
calling fiber in place. After the block, level= had no effect on that
fiber, and the fiber could not be garbage collected while the logger was
alive.

Co-authored-by: Claude Mythos 5.2 <noreply@anthropic.com>
@wtn

wtn commented Nov 16, 2026

Copy link
Copy Markdown
Author

Closing in favor of #169.

@wtn wtn closed this Nov 16, 2026
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.

Memory leak when thread is terminated after using logger.with_level

1 participant