Skip to content

Commit d7fb0a1

Browse files
committed
fix(create-server-hack): add domain clear, avoid memory leak
1 parent c04f0c0 commit d7fb0a1

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

lib/core/runtime/create-server.hack.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ export const httpCreateServerHack = (): void => {
5252
socketConnect: start
5353
} as RequestLog["timestamps"];
5454

55+
// Creating a domain and wrapping the execution.
56+
const d = domain.create();
57+
58+
d.add(req);
59+
d.add(res);
60+
61+
const clearDomain = (): void => {
62+
d.remove(req);
63+
d.remove(res);
64+
65+
while (process.domain) {
66+
(process.domain as domain.Domain).exit();
67+
}
68+
};
69+
5570
const requestInfo = captureIncoming(req);
5671

5772
res.writeHead = ((fn): typeof res.writeHead => (
@@ -123,6 +138,8 @@ export const httpCreateServerHack = (): void => {
123138
timestamps
124139
} as RequestLog;
125140

141+
clearDomain();
142+
126143
eventBus.emit(EVENT_LIST.RESPONSE_FINISH, {
127144
req, res, context
128145
});
@@ -138,11 +155,6 @@ export const httpCreateServerHack = (): void => {
138155
});
139156
});
140157

141-
// Creating a domain and wrapping the execution.
142-
const d = domain.create();
143-
144-
d.add(req);
145-
d.add(res);
146158
d.run(() => {
147159
// 初始化一下 Context
148160
currentContext();

0 commit comments

Comments
 (0)