Skip to content

Commit 0b04722

Browse files
Close HTTP response on unhandled request timeout (#2007)
* Close HTTP response on unhandled request timeout This commit updates the defaultUnhandledRequestHandler to close the HTTP response when an incoming event is not acknowledged within the expected timeframe. This change ensures that resources are not left hanging and that the server correctly indicates to the client that the event was not processed as expected. By sending a 503 Service Unavailable status code and ending the response, we provide clearer semantics and prevent potential resource leaks on the server. * Update error code as per code review feedback
1 parent 5587d01 commit 0b04722

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

src/receivers/HTTPModuleFunctions.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ describe('HTTPModuleFunctions', async () => {
318318
request,
319319
response,
320320
});
321+
assert.isTrue(writeHead.calledWith(404));
321322
});
322323
});
323324
});

src/receivers/HTTPModuleFunctions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,18 @@ export class HTTPModuleFunctions {
219219
// Developers can customize this behavior by passing unhandledRequestHandler to the constructor
220220
// Note that this method cannot be an async function to align with the implementation using setTimeout
221221
public static defaultUnhandledRequestHandler(args: ReceiverUnhandledRequestHandlerArgs): void {
222-
const { logger } = args;
222+
const { logger, response } = args;
223223
logger.error(
224224
'An incoming event was not acknowledged within 3 seconds. ' +
225225
'Ensure that the ack() argument is called in a listener.',
226226
);
227+
228+
// Check if the response has already been sent
229+
if (!response.headersSent) {
230+
// If not, set the status code and end the response to close the connection
231+
response.writeHead(404); // Not Found
232+
response.end();
233+
}
227234
}
228235
}
229236

0 commit comments

Comments
 (0)