Skip to content

Avoid mutating global STDOUT & STDERR (#1837)#1

Open
MitchLewis930 wants to merge 1 commit intopr_051_beforefrom
pr_051_after
Open

Avoid mutating global STDOUT & STDERR (#1837)#1
MitchLewis930 wants to merge 1 commit intopr_051_beforefrom
pr_051_after

Conversation

@MitchLewis930
Copy link
Copy Markdown

PR_051

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Jan 30, 2026

Greptile Overview

Greptile Summary

Modified Puma::Events#initialize to duplicate stdout/stderr parameters before storing them, preventing mutation of global STDOUT and STDERR constants when setting sync mode.

  • Changed @stdout = stdout to @stdout = stdout.dup (and same for stderr) in lib/puma/events.rb:32-33
  • Updated tests to verify that duplication works correctly and that global constants remain unaffected
  • Added test verifying that setting sync on Events object doesn't mutate the original STDOUT.sync
  • Required workaround in test_tcp_logger.rb to re-assign @stdout for test output capture to work

The change prevents side effects where creating a Puma::Events object would permanently modify the global STDOUT and STDERR sync settings, affecting other code in the same process.

Confidence Score: 4/5

  • Safe to merge with minor comment typo
  • The core change is sound and prevents global state mutation, tests properly verify the new behavior, but there's a typo in a test comment and the workaround pattern in test_tcp_logger.rb suggests potential brittleness in testing approach
  • test/test_tcp_logger.rb has a comment typo that should be fixed

Important Files Changed

Filename Overview
lib/puma/events.rb Changed to duplicate stdout/stderr before storing to avoid mutating global IO objects
test/test_events.rb Updated tests to verify duplication behavior and that sync settings don't affect globals
test/test_tcp_logger.rb Added workaround to ensure test output can be captured after duplication change

Sequence Diagram

sequenceDiagram
    participant App as Application Code
    participant Events as Puma::Events
    participant STDOUT as STDOUT (constant)
    participant StdoutDup as STDOUT.dup
    
    Note over App,STDOUT: Before PR (mutates global)
    App->>Events: new(STDOUT, STDERR)
    Events->>STDOUT: @stdout = STDOUT
    Events->>STDOUT: @stdout.sync = true
    Note over STDOUT: STDOUT.sync mutated globally!
    
    Note over App,StdoutDup: After PR (no mutation)
    App->>Events: new(STDOUT, STDERR)
    Events->>STDOUT: stdout.dup
    STDOUT->>StdoutDup: create duplicate
    Events->>StdoutDup: @stdout = dup
    Events->>StdoutDup: @stdout.sync = true
    Note over STDOUT: STDOUT.sync unchanged
    Note over StdoutDup: Only dup is modified
Loading

Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

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

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread test/test_tcp_logger.rb
# in lib/puma/launcher.rb:85
# Puma::Events is default tcp_logger for cluster mode
logger = Puma::Events.new(STDOUT, STDERR)
logger.instance_variable_set(:@stdout, $stdout) # ensure capture_process_io has access to the loggers output
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

typo in comment: 'capture_process_io' should be 'capture_subprocess_io'

Suggested change
logger.instance_variable_set(:@stdout, $stdout) # ensure capture_process_io has access to the loggers output
logger.instance_variable_set(:@stdout, $stdout) # ensure capture_subprocess_io has access to the loggers output
Prompt To Fix With AI
This is a comment left during a code review.
Path: test/test_tcp_logger.rb
Line: 21:21

Comment:
typo in comment: 'capture_process_io' should be 'capture_subprocess_io'

```suggestion
    logger.instance_variable_set(:@stdout, $stdout) # ensure capture_subprocess_io has access to the loggers output
```

How can I resolve this? If you propose a fix, please make it concise.

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.

2 participants