@@ -121,7 +121,16 @@ static boolean currentThreadIsVirtual() {
121121 }
122122 try {
123123 return (boolean ) IS_VIRTUAL .invokeExact (Thread .currentThread ());
124- } catch (Throwable ignoredFallBackToPooled ) {
124+ } catch (RuntimeException | Error fatalMustPropagate ) {
125+ // JVM Errors (OutOfMemoryError, StackOverflowError, …) and runtime
126+ // exceptions are never the reflective-fallback case — let them
127+ // propagate instead of silently reporting "not virtual".
128+ throw fatalMustPropagate ;
129+ } catch (Throwable reflectiveFailureFallBackToPooled ) {
130+ // MethodHandle.invokeExact is declared `throws Throwable`; the only
131+ // residual checked failure here is a reflective/linkage problem
132+ // resolving Thread.isVirtual() — fall back to the non-virtual
133+ // (pooled) path, preserving the prior behavior.
125134 return false ;
126135 }
127136 }
@@ -243,17 +252,17 @@ static ByteBuffer dispatchDirectPooled(
243252 // request (> cap): byte[] fallback is safe for any method
244253 // because no dispatch has run yet. The reusable header buffer
245254 // is consumed here, before any other fillHeaderJson call.
246- return ByteBuffer .wrap (
247- VesperaBridge .dispatchBytes (
248- VesperaWireCodec .assembleWire (hdr .backingArray (), headerLen , bodyBytes )))
249- .asReadOnlyBuffer ();
255+ byte [] wire = VesperaWireCodec .assembleWire (hdr .backingArray (), headerLen , bodyBytes );
256+ VesperaWireCodec .shrinkHeaderBufferIfOversized (hdr );
257+ return ByteBuffer .wrap (VesperaBridge .dispatchBytes (wire )).asReadOnlyBuffer ();
250258 }
251259 ByteBuffer [] pool = directPool ();
252260 if (pool [0 ].capacity () < total ) {
253261 pool [0 ] = ByteBuffer .allocateDirect (grownCapacity (total ));
254262 }
255263 // Consume the reusable header buffer into the pooled direct buffer.
256264 int written = VesperaWireCodec .assembleInto (hdr .backingArray (), headerLen , bodyBytes , pool [0 ]);
265+ VesperaWireCodec .shrinkHeaderBufferIfOversized (hdr );
257266 if (written != total ) {
258267 throw new IllegalStateException (
259268 "assembleInto wrote " + written + ", expected " + total );
@@ -289,16 +298,16 @@ static ByteBuffer dispatchDirectPooled(
289298 int headerLen = hdr .size ();
290299 int total = VesperaWireCodec .wireTotalLength (headerLen , bodyBytes .length );
291300 if (currentThreadIsVirtual || total > DIRECT_MAX_CAPACITY ) {
292- return ByteBuffer .wrap (
293- VesperaBridge .dispatchBytes (
294- VesperaWireCodec .assembleWire (hdr .backingArray (), headerLen , bodyBytes )))
295- .asReadOnlyBuffer ();
301+ byte [] wire = VesperaWireCodec .assembleWire (hdr .backingArray (), headerLen , bodyBytes );
302+ VesperaWireCodec .shrinkHeaderBufferIfOversized (hdr );
303+ return ByteBuffer .wrap (VesperaBridge .dispatchBytes (wire )).asReadOnlyBuffer ();
296304 }
297305 ByteBuffer [] pool = directPool ();
298306 if (pool [0 ].capacity () < total ) {
299307 pool [0 ] = ByteBuffer .allocateDirect (grownCapacity (total ));
300308 }
301309 int written = VesperaWireCodec .assembleInto (hdr .backingArray (), headerLen , bodyBytes , pool [0 ]);
310+ VesperaWireCodec .shrinkHeaderBufferIfOversized (hdr );
302311 if (written != total ) {
303312 throw new IllegalStateException (
304313 "assembleInto wrote " + written + ", expected " + total );
0 commit comments