Skip to content

Commit 82763c8

Browse files
AsaAyerssindresorhus
authored andcommitted
Clear the progress interval if the socket has been destroyed (#469)
1 parent b465f21 commit 82763c8

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ function requestAsEventEmitter(opts) {
198198
const uploadEventFrequency = 150;
199199

200200
progressInterval = setInterval(() => {
201+
if (socket.destroyed) {
202+
clearInterval(progressInterval);
203+
return;
204+
}
205+
201206
const lastUploaded = uploaded;
202207
const headersSize = Buffer.byteLength(req._header);
203208
uploaded = socket.bytesWritten - headersSize;

test/socket-destroyed.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from 'ava';
2+
import got from '..';
3+
4+
test.serial('clear the progressInterval if the socket has been destroyed', async t => {
5+
const handlesComingFromAVA = 2;
6+
const err = await t.throws(got(`http://127.0.0.1:55555`, {retry: 0}));
7+
t.is(process._getActiveHandles().length - handlesComingFromAVA, 2);
8+
t.is(err.code, 'ECONNREFUSED');
9+
});

0 commit comments

Comments
 (0)