@@ -250,8 +250,33 @@ public final void handleCancel() {
250
250
251
251
@ Override
252
252
public void handleNext (ByteBuf followingFrame , boolean hasFollows , boolean isLastPayload ) {
253
- final CompositeByteBuf frames =
254
- ReassemblyUtils .addFollowingFrame (this .frames , followingFrame , this .maxInboundPayloadSize );
253
+ final CompositeByteBuf frames ;
254
+ try {
255
+ frames =
256
+ ReassemblyUtils .addFollowingFrame (
257
+ this .frames , followingFrame , this .maxInboundPayloadSize );
258
+ } catch (IllegalStateException t ) {
259
+ // if subscription is null, it means that streams has not yet reassembled all the fragments
260
+ // and fragmentation of the first frame was cancelled before
261
+ S .lazySet (this , Operators .cancelledSubscription ());
262
+
263
+ this .requesterResponderSupport .remove (this .streamId , this );
264
+
265
+ CompositeByteBuf framesToRelease = this .frames ;
266
+ this .frames = null ;
267
+ framesToRelease .release ();
268
+
269
+ logger .debug ("Reassembly has failed" , t );
270
+
271
+ // sends error frame from the responder side to tell that something went wrong
272
+ final ByteBuf errorFrame =
273
+ ErrorFrameCodec .encode (
274
+ this .allocator ,
275
+ this .streamId ,
276
+ new CanceledException ("Failed to reassemble payload. Cause: " + t .getMessage ()));
277
+ this .sendProcessor .onNext (errorFrame );
278
+ return ;
279
+ }
255
280
256
281
if (!hasFollows ) {
257
282
this .frames = null ;
@@ -260,17 +285,21 @@ public void handleNext(ByteBuf followingFrame, boolean hasFollows, boolean isLas
260
285
payload = this .payloadDecoder .apply (frames );
261
286
frames .release ();
262
287
} catch (Throwable t ) {
288
+ S .lazySet (this , Operators .cancelledSubscription ());
289
+ this .done = true ;
290
+
291
+ this .requesterResponderSupport .remove (this .streamId , this );
292
+
263
293
ReferenceCountUtil .safeRelease (frames );
294
+
264
295
logger .debug ("Reassembly has failed" , t );
265
296
266
- S .lazySet (this , Operators .cancelledSubscription ());
267
- this .done = true ;
268
297
// sends error frame from the responder side to tell that something went wrong
269
298
final ByteBuf errorFrame =
270
299
ErrorFrameCodec .encode (
271
300
this .allocator ,
272
301
this .streamId ,
273
- new CanceledException ("Failed to reassemble payload. Cause" + t .getMessage ()));
302
+ new CanceledException ("Failed to reassemble payload. Cause: " + t .getMessage ()));
274
303
this .sendProcessor .onNext (errorFrame );
275
304
return ;
276
305
}
0 commit comments