Skip to content

Commit 057f362

Browse files
committed
Retain llhttp callback procs to fix intermittent JRuby hang
On JRuby, the full test suite would intermittently hang forever in Connection#read_more waiting for response data that had already been received and consumed from the socket. llhttp-ffi assigns per-parser callback procs into an FFI::Struct (LLHttp::Callbacks) and keeps no other reference to them. JRuby's FFI, unlike MRI's, does not retain Ruby references to procs assigned into struct callback fields, so the procs become garbage as soon as LLHttp::Parser#initialize returns. Once the JVM garbage-collects them, the native callback trampolines are reclaimed and llhttp_execute completes successfully without invoking any callbacks. The parser then reports no progress, so read_headers! loops back into a blocking readpartial that never returns. Prepend a module to LLHttp::Callbacks that retains each assigned proc on the struct instance, tying the trampolines' lifetime to the parser that owns them. A minimal repro (LLHttp::Parser + java.lang.System.gc) fails on the first iteration without this fix and survives thousands of iterations with it; the full suite passed repeated JRuby runs. This should ultimately be fixed upstream in llhttp-ffi.
1 parent a65a8d2 commit 057f362

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- On JRuby, HTTP responses could intermittently hang forever (or time out)
13+
waiting for data that had already been received. JRuby's FFI does not retain
14+
Ruby references to procs assigned into `FFI::Struct` callback fields, so the
15+
JVM could garbage-collect llhttp's native callback trampolines while a parser
16+
was still in use, after which `llhttp_execute` succeeded without invoking any
17+
callbacks. Callback procs are now retained for the lifetime of the parser.
18+
1019
## [6.0.4] - 2026-07-14
1120

1221
### Fixed

‎lib/http/response/parser.rb‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
require "llhttp"
44

5+
# :nocov:
6+
if RUBY_ENGINE == "jruby"
7+
# JRuby's FFI does not retain Ruby references to procs assigned into
8+
# FFI::Struct callback fields, so the JVM may garbage-collect the native
9+
# callback trampolines while the parser is still in use, after which
10+
# llhttp_execute succeeds without invoking any callbacks. Retain each
11+
# assigned proc on the struct so the trampolines live as long as the
12+
# parser that owns them.
13+
LLHttp::Callbacks.prepend(Module.new do
14+
def []=(key, value)
15+
(@retained_callback_procs ||= []) << value
16+
super
17+
end
18+
end)
19+
end
20+
# :nocov:
21+
522
module HTTP
623
class Response
724
# HTTP response parser backed by LLHttp

0 commit comments

Comments
 (0)