我有一个async函数,它创建一个读流并将其写入http响应,如下所示:
let handle = await getFile();
let stream = handle.createReadStream();
let stat = await handle.stat();
res.setHeader("content-length", stat.size);
res.setHeader("content-type", "application/octet");
res.statusCode = 200;
stream.pipe(res);
stream.on('close', () => {
handle.close();
res.end();
});
正如您所看到的,当流关闭时,我关闭了句柄,同时也结束了响应。然而,我偶尔会看到node记录如下内容:
(node:54119) Warning: Closing file descriptor 21 on garbage collection
(Use `node --trace-warnings ...` to show where the warning was created)
(node:54119) [DEP0137] DeprecationWarning: Closing a FileHandle object on garbage collection is deprecated. Please close FileHandle objects explicitly using FileHandle.prototype.close(). In the future, an error will be thrown if a file descriptor is closed during garbage collection.
我的HTTP响应中没有错误,并且我已经通过日志验证了我的handle.close()
正在为每个请求调用。为什么node认为我有未关闭的文件描述符?
1条答案
按热度按时间qv7cva1a1#
这是因为我的async函数在流完成写入之前返回 *。我用一个承诺来 Package 它,等待它: