@@ -16,13 +16,16 @@ type Stream* = ref object
1616 closed* : AsyncEvent # This is called when on_close callback is executed
1717 isEof* : bool # Received a FIN from remote
1818 toWrite* : seq [WriteTask ]
19+ shouldClose* : Future [void ].Raising ([CancelledError ])
1920
2021proc new * (T: typedesc [Stream ], quicStream: ptr lsquic_stream_t = nil ): T =
21- Stream (
22+ let s = Stream (
2223 quicStream: quicStream,
2324 incoming: newAsyncQueue [seq [byte ]](),
2425 closed: newAsyncEvent (),
2526 )
27+ GC_ref (s) # Keep it pinned until stream_if.on_close is executed
28+ s
2629
2730proc abortPendingWrites * (stream: Stream , reason: string = " " ) =
2831 for pendingWrite in stream.toWrite.mitems:
@@ -33,27 +36,38 @@ proc abortPendingWrites*(stream: Stream, reason: string = "") =
3336proc abort * (stream: Stream ) =
3437 if stream.closeWrite and stream.isEof:
3538 if not stream.closed.isSet ():
39+ echo " FIRING CLOSE 1"
3640 stream.closed.fire ()
3741 stream.abortPendingWrites (" stream aborted" )
3842 return
3943
44+ echo " CLOSING ON ABORT"
4045 let ret = lsquic_stream_close (stream.quicStream)
4146 if ret != 0 :
4247 error " could not abort stream" , streamId = lsquic_stream_id (stream.quicStream)
4348
4449 stream.closeWrite = true
4550 stream.isEof = true
4651 stream.abortPendingWrites (" stream aborted" )
52+ echo " FIRING CLOSE 2"
4753 stream.closed.fire ()
4854
49- proc close * (stream: Stream ) =
55+ proc close * (stream: Stream ) {. async : (raises: [ StreamError , CancelledError ]).} =
5056 if stream.closeWrite:
5157 return
58+ echo " CALLED CLOSE!!!!!!!!!!!!!!!!!!"
59+ if stream.toWrite.len != 0 :
60+ stream.shouldClose = Future [void ].Raising ([CancelledError ]).init ()
61+
62+ echo " AWAITING FOR CLOSE"
63+ await stream.shouldClose
5264
5365 # Closing only the write side
66+ echo " SHUTDOWN WRITE"
5467 let ret = lsquic_stream_shutdown (stream.quicStream, 1 )
5568 if ret == 0 :
5669 if stream.isEof:
70+ echo " CLOSE DUE TO EOF"
5771 if lsquic_stream_close (stream.quicStream) != 0 :
5872 stream.abort ()
5973 raise newException (StreamError , " could not close the stream" )
@@ -68,22 +82,26 @@ proc read*(
6882 return @ []
6983
7084 if lsquic_stream_wantread (stream.quicStream, 1 ) == - 1 :
85+ echo " ABORTING"
7186 stream.abort ()
7287 raise newException (StreamError , " could not set wantread" )
7388
7489 let incomingFut = stream.incoming.get ()
7590 let closedFut = stream.closed.wait ()
7691 let raceFut = await race (closedFut, incomingFut)
7792 if raceFut == closedFut:
93+ echo " STREAM GOT CLOSED"
7894 await incomingFut.cancelAndWait ()
7995 stream.isEof = true
8096 stream.closeWrite = true
81- raise newException (StreamError , " stream closed" )
97+ raise newException (StreamError , " stream closed 1 " )
8298
8399 let incoming = await incomingFut
84100 if incoming.len == 0 :
101+ echo " INCOMING LEN IS 0"
85102 if stream.closeWrite:
86103 # We were already closed for write. Close the stream completely
104+ echo " CLOSING AFTER READ IS EOF, and WRITE WAS CALLED BEFORE"
87105 if lsquic_stream_close (stream.quicStream) != 0 :
88106 stream.abort ()
89107 raise newException (StreamError , " could not close the stream" )
@@ -95,15 +113,16 @@ proc write*(
95113 stream: Stream , data: seq [byte ]
96114) {.async : (raises: [CancelledError , StreamError ]).} =
97115 if stream.closeWrite:
98- raise newException (StreamError , " stream is closed" )
116+ raise newException (StreamError , " stream closed 3 " )
99117
100118 let closedFut = stream.closed.wait ()
101119 let doneFut = Future [void ].Raising ([CancelledError , StreamError ]).init ()
102120 stream.toWrite.add (WriteTask (data: data, doneFut: doneFut))
103121 discard lsquic_stream_wantwrite (stream.quicStream, 1 )
104122 let raceFut = await race (closedFut, doneFut)
105123 if raceFut == closedFut:
106- doneFut.fail (newException (StreamError , " stream closed" ))
124+ if not doneFut.finished:
125+ doneFut.fail (newException (StreamError , " stream closed 2" ))
107126 stream.closeWrite = true
108127
109128 await doneFut
0 commit comments