@@ -272,11 +272,7 @@ def initialize(options = {})
272272 end
273273
274274 @connection = res [ :conn_ptr ]
275- # Serializes `close` so two threads cannot both capture the handle and
276- # double-decrement its Arc refcount (issue #212). We use `Mutex#try_lock`
277- # in `close`, not `#synchronize`, so trap-context callers still work:
278- # only `#lock`/`#synchronize` raise ThreadError from a trap, `#try_lock`
279- # returns false and moves on.
275+ # Lock for serializing close(). See `close` for why try_lock is used.
280276 @close_lock = Mutex . new
281277 Bindings . free_connection_response ( response_ptr )
282278
@@ -298,24 +294,7 @@ def initialize(options = {})
298294 @in_multi_block = false
299295 end
300296
301- # Closes the client and frees the native connection. Idempotent and
302- # thread-safe: `@close_lock.try_lock` lets exactly one caller free the
303- # handle, while every other concurrent `close` (and every subsequent one)
304- # is a no-op. Without this, two threads could both read `@connection`
305- # before either nulled it, both call `close_client`, and double-decrement
306- # the Arc refcount - which is UB per Rust (issue #212).
307- #
308- # `try_lock` (not `synchronize`) is deliberate: `Mutex#synchronize` /
309- # `#lock` raise ThreadError from a trap context, but `#try_lock` does not.
310- # That keeps the standard `Signal.trap("TERM") { client.close }` shutdown
311- # idiom working - the trap either wins the lock and closes, or another
312- # thread has already closed and it silently returns.
313- #
314- # In-flight commands are safe: every glide-ffi command entry point does
315- # `Arc::increment_strong_count` on the handle before using it, and
316- # `close_client` only decrements, so the native ClientAdapter outlives any
317- # request still executing and is dropped once the last one finishes.
318- # Verified with 12 concurrent blocking calls held open across a `close`.
297+ # Closes the client and frees the native connection.
319298 def close
320299 return unless @close_lock &.try_lock
321300
0 commit comments