Delete the context when an event loop is done
diff --git a/src/trace/context/ContextManager.ts b/src/trace/context/ContextManager.ts
index 3360031..e49bbb0 100644
--- a/src/trace/context/ContextManager.ts
+++ b/src/trace/context/ContextManager.ts
@@ -19,11 +19,19 @@
import Context from '../../trace/context/Context';
import SpanContext from '../../trace/context/SpanContext';
-import { executionAsyncId } from 'async_hooks';
+import { executionAsyncId, createHook } from 'async_hooks';
class ContextManager {
contextKeyedByAsyncId: { [asyncId: number]: Context } = {};
+ constructor() {
+ createHook({
+ destroy: (asyncId: number) => {
+ delete this.contextKeyedByAsyncId[asyncId];
+ },
+ }).enable();
+ }
+
get current(): Context {
const thisAsyncId = executionAsyncId();