Skip to content

Commit

Permalink
Prevent unhandled exception when streams are closing
Browse files Browse the repository at this point in the history
and the session is still busy

Fixes #36
  • Loading branch information
szmarczak committed Apr 21, 2020
1 parent d621fac commit 68e4323
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
14 changes: 9 additions & 5 deletions source/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ const addSession = (where, name, session) => {
};

const getSessions = (where, name, normalizedOrigin) => {
if (Reflect.has(where, name)) {
return where[name].filter(session => {
return !session.closed && !session.destroyed && session[kOriginSet].includes(normalizedOrigin);
});
if (!Reflect.has(where, name)) {
return [];
}

return [];
return where[name].filter(session => {
return !session.closed && !session.destroyed && session[kOriginSet].includes(normalizedOrigin);
});
};

// See https://tools.ietf.org/html/rfc8336
Expand Down Expand Up @@ -108,6 +108,10 @@ const closeCoveredSessions = (where, name, session) => {

// This is basically inverted `closeCoveredSessions(...)`.
const closeSessionIfCovered = (where, name, coveredSession) => {
if (!Reflect.has(where, name)) {
return;
}

for (const session of where[name]) {
if (
coveredSession[kOriginSet].length < session[kOriginSet].length &&
Expand Down
19 changes: 19 additions & 0 deletions test/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,25 @@ test('sessions can be manually overloaded', singleRequestWrapper, async (t, serv
agent.destroy();
});

test('sessions can be manually overloaded #2', singleRequestWrapper, async (t, server) => {
server.get('/', (request, response) => {
response.end();
});

const agent = new Agent();

const session = await agent.getSession(server.url);
const requests = [session.request(), session.request()];

requests[0].end();

await pEvent(requests[0], 'close');

t.pass();

agent.destroy();
});

test('`.settings` property', wrapper, async (t, server) => {
const agent = new Agent();
agent.settings.maxHeaderListSize = 100;
Expand Down

0 comments on commit 68e4323

Please sign in to comment.