@@ -214,15 +214,18 @@ Connection.prototype._insert = function _insert(stream, priority) {
214214} ;
215215
216216Connection . prototype . _reprioritize = function _reprioritize ( stream , priority ) {
217+ this . _removePrioritisedStream ( stream ) ;
218+ this . _insert ( stream , priority ) ;
219+ } ;
220+
221+ Connection . prototype . _removePrioritisedStream = function _removePrioritisedStream ( stream ) {
217222 var bucket = this . _streamPriorities [ stream . _priority ] ;
218223 var index = bucket . indexOf ( stream ) ;
219224 assert ( index !== - 1 ) ;
220225 bucket . splice ( index , 1 ) ;
221226 if ( bucket . length === 0 ) {
222227 delete this . _streamPriorities [ stream . _priority ] ;
223228 }
224-
225- this . _insert ( stream , priority ) ;
226229} ;
227230
228231// Creating an *inbound* stream with the given ID. It is called when there's an incoming frame to
@@ -246,9 +249,18 @@ Connection.prototype.createStream = function createStream() {
246249 var stream = new Stream ( this . _log , this ) ;
247250 this . _allocatePriority ( stream ) ;
248251
252+ stream . on ( 'end' , this . _removeStream . bind ( this , stream ) ) ;
253+
249254 return stream ;
250255} ;
251256
257+ Connection . prototype . _removeStream = function _removeStream ( stream ) {
258+ this . _log . trace ( 'Removing outbound stream.' ) ;
259+
260+ delete this . _streamIds [ stream . id ] ;
261+ this . _removePrioritisedStream ( stream ) ;
262+ } ;
263+
252264// Multiplexing
253265// ------------
254266
@@ -290,7 +302,7 @@ priority_loop:
290302 // 2. if there's no frame, skip this stream
291303 // 3. if forwarding this frame would make `streamCount` greater than `streamLimit`, skip
292304 // this stream
293- // 4. adding stream to the bucket of the next round
305+ // 4. adding stream to the bucket of the next round unless it has ended
294306 // 5. assigning an ID to the frame (allocating an ID to the stream if there isn't already)
295307 // 6. if forwarding a PUSH_PROMISE, allocate ID to the promised stream
296308 // 7. forwarding the frame, changing `streamCount` as appropriate
@@ -299,6 +311,7 @@ priority_loop:
299311 while ( bucket . length > 0 ) {
300312 for ( var index = 0 ; index < bucket . length ; index ++ ) {
301313 var stream = bucket [ index ] ;
314+ if ( ! stream || ! stream . upstream ) continue ;
302315 var frame = stream . upstream . read ( ( this . _window > 0 ) ? this . _window : - 1 ) ;
303316
304317 if ( ! frame ) {
@@ -308,7 +321,11 @@ priority_loop:
308321 continue ;
309322 }
310323
311- nextBucket . push ( stream ) ;
324+ if ( ! stream . _ended ) {
325+ nextBucket . push ( stream ) ;
326+ } else {
327+ delete this . _streamIds [ stream . id ] ;
328+ }
312329
313330 if ( frame . stream === undefined ) {
314331 frame . stream = stream . id || this . _allocateId ( stream ) ;
0 commit comments