@@ -31,23 +31,25 @@ module.exports = (registryService, pluginManager, context) => {
3131
3232 app . addHook ( 'onRequest' , ( req , reply , done ) => {
3333 context . run ( { request : req , requestId : reportingPluginManager . getRequestId ( ) ?? request . id } , async ( ) => {
34+ let doneWithContext ;
35+
3436 try {
3537 const asyncResource = new AsyncResource ( 'fastify-request-context' ) ;
3638 req [ asyncResourceSymbol ] = asyncResource ;
37- const doneWithContext = ( ) => asyncResource . runInAsyncScope ( done , req . raw ) ;
39+ doneWithContext = ( ) => asyncResource . runInAsyncScope ( done , req . raw ) ;
3840
3941 const { url, method } = req . raw ;
4042 accessLogger . logRequest ( ) ;
4143
4244 if ( ! [ 'GET' , 'OPTIONS' , 'HEAD' ] . includes ( method ) ) {
4345 logger . warn ( `Request method ${ method } is not allowed for url ${ url } ` ) ;
4446 reply . code ( 405 ) . send ( { message : 'Method Not Allowed' } ) ;
45- return ;
47+ return doneWithContext ( ) ;
4648 }
4749
4850 if ( isDataUri ( url ) ) {
4951 reply . code ( 400 ) . send ( { message : 'Bad Request: Data URIs are not valid HTTP paths' } ) ;
50- return ;
52+ return doneWithContext ( ) ;
5153 }
5254
5355 req . raw . ilcState = { } ;
@@ -67,9 +69,15 @@ module.exports = (registryService, pluginManager, context) => {
6769
6870 await i18nOnRequest ( req , reply ) ;
6971
70- doneWithContext ( ) ;
72+ return doneWithContext ( ) ;
7173 } catch ( error ) {
7274 errorHandler . handleError ( error , req , reply ) ;
75+
76+ if ( typeof doneWithContext === 'function' ) {
77+ doneWithContext ( ) ;
78+ } else {
79+ done ( ) ;
80+ }
7381 }
7482 } ) ;
7583 } ) ;
0 commit comments