Repository navigation
deps: parser v0.13.1, so END { } runs instead of killing the process - #809
Merged
Merged
Conversation
`END { }` -- ordinary Ruby, in the language since 1.0 -- ended the process with
an uncontained Go panic when the handler ran at exit:
$ echo 'END { puts "E" }' > p.rb && rbgo p.rb
panic: runtime error: slice bounds out of range [:1] with capacity 0
$ echo $?
2
For a program that is a crash instead of an answer. For an EMBEDDER it is worse:
ruby.Run does not return an error, it takes the host process down, and nothing
the host writes around the call prevents it. #805.
The cause was upstream and is one line. `END { }` desugars to `at_exit { }`, and
the Block the parser built left SplatIndex at Go's ZERO VALUE -- which is a
perfectly good index, where that field's "no splat" value is -1. So the block
announced a *splat parameter at position 0 with no parameters to hold it, and
this compiler's rest-binding believed it and read past an empty environment.
go-ruby-parser/parser#52, released as v0.13.1 (a patch: no exported API moved).
The regression test lives here as well as upstream, and that is the point rather
than belt and braces: the defect was INVISIBLE IN THE AST. The parser's own
suite was green throughout, because a parser does not run what it builds -- the
entire cost landed on the consumer that lowers a Block. A downgrade of this
dependency brings the crash back, and this is what notices.
It asserts the ORDERING too, not just the absence of a crash: END and at_exit
handlers run LIFO, measured on both engines (MRI 4.0.7 and rbgo agree, "second
registered" then "first registered"). BEGIN and at_exit are in the same test as
controls, so a regression in any of the three is attributable to the right half.
The README row added this morning said END crashes the process. It did, and this
is the commit that stops it, so the row goes in the same change -- a document
that is wrong on the day it ships is the thing yesterday's doc sweep was about.
Closes #805.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Oct 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #805.
What it was
END { }has been in Ruby since 1.0. For a program this is a crash instead ofan answer; for an embedder it is worse —
ruby.Rundoes not return anerror, it takes the host process down, and nothing the host writes around the
call prevents it.
A Go zero value that means something
END { }desugars toat_exit { }, and theBlockthe parser built leftSplatIndexat Go's zero value. Zero is a perfectly good index, and thatfield's "no splat" value is
-1:So the block announced a
*splatparameter at position 0 with no parameters tohold it, and this compiler's rest-binding believed it and read past an empty
environment.
One line upstream:
go-ruby-parser/parser#52, released as v0.13.1 — apatch, as
gorelease -base v0.13.0reports, since no exported API moved.END { puts "E" }EEputs "main"+ENDmainEmainEBEGIN+main+ENDBmainEBmainEWhy the regression test lives here too
Not belt and braces — the defect was invisible in the AST. The parser's own
suite was green throughout, because a parser does not run what it builds: the
entire cost landed on the consumer that lowers a
Block. A downgrade of thisdependency brings the crash back, and this is what notices.
It asserts the ordering as well as the absence of a crash:
ENDandat_exithandlers run LIFO, measured on both engines — MRI 4.0.7 and rbgo agreeon
second registeredthenfirst registered. A test that only checked "did itcrash" would pass on a handler that never ran.
BEGINandat_exitsit in thesame test as controls, so a regression in any of the three is attributable to
the right half.
The README row goes in the same change
The row added this morning says
END { }crashes the process. It did, andthis is the commit that stops it — so the row goes now. A document that is wrong
on the day it ships is precisely what #801 was about.
🤖 Generated with Claude Code