|
| 1 | +// Flags: --experimental-quic --experimental-stream-iter --no-warnings |
| 2 | + |
| 3 | +// Regression test for https://github.com/nodejs/node/issues/64290 |
| 4 | +// When a stream writer has a pending drain promise and the remote peer |
| 5 | +// resets the stream, the rejected drain promise must NOT surface as an |
| 6 | +// unhandled rejection. |
| 7 | + |
| 8 | +import{hasQuic,skip,mustCall,mustNotCall}from'../common/index.mjs'; |
| 9 | +importassertfrom'node:assert'; |
| 10 | +import{setImmediateastick}from'node:timers/promises'; |
| 11 | + |
| 12 | +if(!hasQuic){ |
| 13 | +skip('QUIC is not enabled'); |
| 14 | +} |
| 15 | + |
| 16 | +const{ listen, connect }=awaitimport('../common/quic.mjs'); |
| 17 | +const{ drainableProtocol }=awaitimport('stream/iter'); |
| 18 | + |
| 19 | +// The test fails if any unhandled rejection fires. |
| 20 | +process.on('unhandledRejection', |
| 21 | +mustNotCall('unexpected unhandled rejection')); |
| 22 | + |
| 23 | +constserverStreamReady=Promise.withResolvers(); |
| 24 | + |
| 25 | +constserverEndpoint=awaitlisten(mustCall((serverSession)=>{ |
| 26 | +serverSession.onstream=mustCall((stream)=>{ |
| 27 | +serverStreamReady.resolve({ stream,session: serverSession}); |
| 28 | +}); |
| 29 | +})); |
| 30 | + |
| 31 | +constclientSession=awaitconnect(serverEndpoint.address); |
| 32 | +awaitclientSession.opened; |
| 33 | + |
| 34 | +conststream=awaitclientSession.createBidirectionalStream(); |
| 35 | +constwriter=stream.writer; |
| 36 | + |
| 37 | +// Write a small initial chunk so the server materializes the stream. |
| 38 | +writer.writeSync(newUint8Array([1])); |
| 39 | + |
| 40 | +const{stream: serverStream,session: serverSession}= |
| 41 | +awaitserverStreamReady.promise; |
| 42 | + |
| 43 | +// Fill the write buffer to create backpressure. After this, |
| 44 | +// writeDesiredSize should be <= 0 and canWrite should be false. |
| 45 | +constchunk=newUint8Array(64*1024); |
| 46 | +while(writer.canWrite){ |
| 47 | +if(!writer.writeSync(chunk))break; |
| 48 | +} |
| 49 | + |
| 50 | +// Create a drain wakeup via the drainable protocol. This simulates |
| 51 | +// what the stream/iter infrastructure does when checking for |
| 52 | +// backpressure. We deliberately do NOT await the returned promise β |
| 53 | +// that is the whole point of the test. |
| 54 | +constdrainPromise=writer[drainableProtocol](); |
| 55 | +assert.ok(drainPromiseinstanceofPromise, |
| 56 | +'expected a drain promise (buffer should be full)'); |
| 57 | + |
| 58 | +// Suppress the expected rejection on both sides' closed promises so |
| 59 | +// they do not interfere with the unhandledRejection check. |
| 60 | +constclientClosed=stream.closed.catch(()=>{}); |
| 61 | +constserverClosed=serverStream.closed.catch(()=>{}); |
| 62 | + |
| 63 | +// Have the server send STOP_SENDING. This triggers kStopSending on |
| 64 | +// the client writer, which rejects the unobserved drain promise. |
| 65 | +// Without the fix this surfaces as an unhandled rejection. |
| 66 | +serverStream.stopSending(1n); |
| 67 | +serverStream.writer.endSync(); |
| 68 | + |
| 69 | +// Give the event loop time to process the frame and fire any |
| 70 | +// unhandled-rejection events. |
| 71 | +awaittick(); |
| 72 | +awaittick(); |
| 73 | + |
| 74 | +// Clean up. |
| 75 | +awaitPromise.all([clientClosed,serverClosed]); |
| 76 | +serverSession.close(); |
| 77 | +awaitclientSession.close(); |
| 78 | +awaitserverEndpoint.close(); |
0 commit comments