Skip to content

Commit 758429c

Browse files
committed
fix: release on error & close
1 parent f1c3339 commit 758429c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ function createError (msg, code, statusCode) {
212212

213213
class ErrorHandler {
214214
constructor () {
215+
this.released = true
215216
this.hasError = false
216217
this.req = null
217218
this.resOrSocket = null
@@ -251,6 +252,11 @@ class ErrorHandler {
251252
}
252253

253254
_release () {
255+
if (this.released) {
256+
return
257+
}
258+
259+
this.released = true
254260
this.hasError = false
255261
this.req = null
256262
this.resOrSocket = null
@@ -261,18 +267,21 @@ class ErrorHandler {
261267

262268
static create (req, resOrSocket, callback) {
263269
const handler = ErrorHandler.pool.pop() || new ErrorHandler()
270+
handler.released = false
264271
handler.hasError = false
265272
handler.req = req
266273
handler.resOrSocket = resOrSocket
267274
handler.callback = callback
268275
handler.req.on('close', handler._release)
276+
handler.req.on('error', handler._release)
269277
return handler._handle
270278
}
271279
}
272280
ErrorHandler.pool = []
273281

274282
class ProxyErrorHandler {
275283
constructor () {
284+
this.released = true
276285
this.hasError = false
277286
this.req = null
278287
this.proxyReq = null
@@ -323,8 +332,13 @@ class ProxyErrorHandler {
323332
}
324333

325334
_release () {
335+
if (this.released) {
336+
return
337+
}
338+
326339
this._abort()
327340

341+
this.released = true
328342
this.hasError = false
329343
this.req = null
330344
this.proxyReq = null
@@ -335,17 +349,20 @@ class ProxyErrorHandler {
335349

336350
static create (req, proxyReq, errorHandler) {
337351
const handler = ProxyErrorHandler.pool.pop() || new ProxyErrorHandler()
352+
handler.released = false
338353
handler.req = req
339354
handler.proxyReq = proxyReq
340355
handler.errorHandler = errorHandler
341356
handler.req.on('close', handler._release)
357+
handler.req.on('error', handler._release)
342358
return handler._handle
343359
}
344360
}
345361
ProxyErrorHandler.pool = []
346362

347363
class ProxyResponseHandler {
348364
constructor () {
365+
this.released = true
349366
this.req = null
350367
this.resOrSocket = null
351368
this.onRes = null
@@ -395,10 +412,15 @@ class ProxyResponseHandler {
395412
}
396413

397414
_release () {
415+
if (this.released) {
416+
return
417+
}
418+
398419
if (this.proxyRes) {
399420
this.proxyRes.destroy()
400421
}
401422

423+
this.released = true
402424
this.req = null
403425
this.resOrSocket = null
404426
this.onRes = null
@@ -410,19 +432,22 @@ class ProxyResponseHandler {
410432

411433
static create (req, resOrSocket, onRes, proxyErrorHandler) {
412434
const handler = ProxyResponseHandler.pool.pop() || new ProxyResponseHandler()
435+
handler.released = false
413436
handler.req = req
414437
handler.resOrSocket = resOrSocket
415438
handler.onRes = onRes
416439
handler.proxyErrorHandler = proxyErrorHandler
417440
handler.proxyRes = null
418441
handler.req.on('close', handler._release)
442+
handler.req.on('error', handler._release)
419443
return handler._handle
420444
}
421445
}
422446
ProxyResponseHandler.pool = []
423447

424448
class ProxyUpgradeHandler {
425449
constructor () {
450+
this.released = true
426451
this.req = null
427452
this.resOrSocket = null
428453
this.proxyErrorHandler = null
@@ -470,13 +495,18 @@ class ProxyUpgradeHandler {
470495
}
471496

472497
_release () {
498+
if (this.released) {
499+
return
500+
}
501+
473502
if (this.proxyRes) {
474503
this.proxyRes.destroy()
475504
}
476505
if (this.proxySocket) {
477506
this.proxySocket.destroy()
478507
}
479508

509+
this.released = true
480510
this.req = null
481511
this.resOrSocket = null
482512
this.proxyErrorHandler = null
@@ -488,10 +518,12 @@ class ProxyUpgradeHandler {
488518

489519
static create (req, resOrSocket, proxyErrorHandler) {
490520
const handler = ProxyUpgradeHandler.pool.pop() || new ProxyUpgradeHandler()
521+
handler.released = false
491522
handler.req = req
492523
handler.resOrSocket = resOrSocket
493524
handler.proxyErrorHandler = proxyErrorHandler
494525
handler.req.on('close', handler._release)
526+
handler.req.on('error', handler._release)
495527
return handler._handle
496528
}
497529
}

0 commit comments

Comments
 (0)