Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use $stderr instead of STDERR for Ractor #57

Merged
merged 1 commit into from
Jan 21, 2025
Merged

Conversation

wanabe
Copy link
Contributor

@wanabe wanabe commented Jan 19, 2025

Hello,

I found that ErrorHighlight::DefaultFormatter.terminal_width can't be called from non-main Ractor.

$ ruby -Ilib -rerror_highlight -e 'Ractor.new { p ErrorHighlight.formatter.terminal_width }.take'
-e:1: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
#<Thread:0x00007fcde7022b98 run> terminated with exception (report_on_exception is true):
/home/wanabe/work/prog/ruby/error_highlight/lib/error_highlight/formatter.rb:61:in 'ErrorHighlight::DefaultFormatter.terminal_width': can not access non-shareable objects in constant ErrorHighlight::DefaultFormatter::STDERR by non-main Ractor. (Ractor::IsolationError)
        from -e:1:in 'block in <main>'
<internal:ractor>:711:in 'Ractor#take': thrown by remote Ractor. (Ractor::RemoteError)
        from -e:1:in '<main>'
/home/wanabe/work/prog/ruby/error_highlight/lib/error_highlight/formatter.rb:61:in 'ErrorHighlight::DefaultFormatter.terminal_width': can not access non-shareable objects in constant ErrorHighlight::DefaultFormatter::STDERR by non-main Ractor. (Ractor::IsolationError)
        from -e:1:in 'block in <main>'

Replacing STDERR to $stderr resolves this issue because $stderr is a ractor-local global variable.

$ ruby -Ilib -rerror_highlight -e 'Ractor.new { p ErrorHighlight.formatter.terminal_width }.take'
-e:1: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
104

How do you like it? Ijust want to solve the issue, so the method can be anything.

@mame
Copy link
Member

mame commented Jan 21, 2025

Thanks, it looks like the Ruby interpreter writes uncaught exception messages to $stderr instead of STDERR

$ ruby -e '$stderr = open(IO::NULL, "w"); 1.time'

$ ruby -e 'STDERR = open(IO::NULL, "w"); 1.time'
-e:1: warning: already initialized constant STDERR
-e:1:in '<main>': undefined method 'time' for an instance of Integer (NoMethodError)
Did you mean?  times

So it seems correct for error_highlight to use $stderr here.

@mame mame merged commit a221a4b into ruby:master Jan 21, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants