GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fs: close fd 0 on discarded FileHandle transfer · nodejs/node@661e23d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_file.cc
  • test/parallel
    • test-worker-message-port-transfer-filehandle-fd0.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_file.cc‎

Copy file name to clipboardExpand all lines: src/node_file.cc
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ std::unique_ptr<worker::TransferData> FileHandle::TransferForMessaging() {
316316
FileHandle::TransferData::TransferData(int fd) : fd_(fd) {}
317317

318318
FileHandle::TransferData::~TransferData() {
319-
if (fd_ > 0) {
319+
if (fd_ >= 0) {
320320
uv_fs_t close_req;
321321
CHECK_NE(fd_, -1);
322322
FS_SYNC_TRACE_BEGIN(close);
Collapse file

‎test/parallel/test-worker-message-port-transfer-filehandle-fd0.js‎

Copy file name to clipboard
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
// A FileHandle whose transfer is discarded has to close its file descriptor.
4+
// Descriptor 0 is an ordinary descriptor once stdin has been closed, so the
5+
// scenario runs in a child process that can afford to lose its stdin.
6+
7+
const common = require('../common');
8+
9+
if (common.isWindows)
10+
common.skip('descriptor numbering after closing stdin is POSIX-specific');
11+
12+
const assert = require('assert');
13+
const fs = require('fs');
14+
15+
if (process.argv[2] === 'child') {
16+
const vm = require('vm');
17+
const { MessageChannel, moveMessagePortToContext } =
18+
require('worker_threads');
19+
20+
(async function() {
21+
fs.closeSync(0);
22+
const fh = await fs.promises.open(__filename);
23+
assert.strictEqual(fh.fd, 0);
24+
25+
const { port1, port2 } = new MessageChannel();
26+
// A port living in another context cannot receive the handle, so the
27+
// message is discarded on delivery and takes the descriptor with it.
28+
const moved = moveMessagePortToContext(port2, vm.createContext());
29+
const discarded = new Promise((resolve) => {
30+
moved.onmessageerror = resolve;
31+
});
32+
moved.start();
33+
34+
port1.postMessage(fh, [ fh ]);
35+
await discarded;
36+
37+
assert.throws(() => fs.fstatSync(0), { code: 'EBADF' });
38+
port1.close();
39+
})().then(common.mustCall());
40+
return;
41+
}
42+
43+
const { spawnSync } = require('child_process');
44+
const result = spawnSync(process.execPath, [ __filename, 'child' ],
45+
{ stdio: [ 'ignore', 'inherit', 'inherit' ] });
46+
assert.strictEqual(result.status, 0);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.