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 — async_hooks: avoid resource reuse by FileHandle · nodejs/node@146ad4e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • async_wrap.cc
    • async_wrap.h
    • node_file.cc
  • test/async-hooks
    • test-filehandle-no-reuse.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/async_wrap.cc‎

Copy file name to clipboardExpand all lines: src/async_wrap.cc
+1-5Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ AsyncWrap::AsyncWrap(Environment* env,
581581
provider_type_ = provider;
582582

583583
// Use AsyncReset() call to execute the init() callbacks.
584-
AsyncReset(execution_async_id, silent);
584+
AsyncReset(object, execution_async_id, silent);
585585
init_hook_ran_ = true;
586586
}
587587

@@ -647,10 +647,6 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
647647
env->destroy_async_id_list()->push_back(async_id);
648648
}
649649

650-
void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
651-
AsyncReset(object(), execution_async_id, silent);
652-
}
653-
654650
// Generalized call for both the constructor and for handles that are pooled
655651
// and reused over their lifetime. This way a new uid can be assigned when
656652
// the resource is pulled out of the pool and put back into use.
Collapse file

‎src/async_wrap.h‎

Copy file name to clipboardExpand all lines: src/async_wrap.h
-3Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ class AsyncWrap : public BaseObject {
168168
double execution_async_id = kInvalidAsyncId,
169169
bool silent = false);
170170

171-
void AsyncReset(double execution_async_id = kInvalidAsyncId,
172-
bool silent = false);
173-
174171
// Only call these within a valid HandleScope.
175172
v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,
176173
int argc,
Collapse file

‎src/node_file.cc‎

Copy file name to clipboardExpand all lines: src/node_file.cc
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,12 @@ int FileHandle::ReadStart() {
369369
if (freelist.size() > 0) {
370370
read_wrap = std::move(freelist.back());
371371
freelist.pop_back();
372-
read_wrap->AsyncReset();
372+
// Use a fresh async resource.
373+
// Lifetime is ensured via AsyncWrap::resource_.
374+
Local<Object> resource = Object::New(env()->isolate());
375+
resource->Set(
376+
env()->context(), env()->handle_string(), read_wrap->object());
377+
read_wrap->AsyncReset(resource);
373378
read_wrap->file_handle_ = this;
374379
} else {
375380
Local<Object> wrap_obj;
Collapse file

‎test/async-hooks/test-filehandle-no-reuse.js‎

Copy file name to clipboard
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const initHooks = require('./init-hooks');
5+
const { checkInvocations } = require('./hook-checks');
6+
const fixtures = require('../common/fixtures');
7+
if (!common.hasCrypto)
8+
common.skip('missing crypto');
9+
const http2 = require('http2');
10+
const assert = require('assert');
11+
const fs = require('fs');
12+
13+
// Checks that the async resource is not reused by FileHandle.
14+
// Test is based on parallel\test-http2-respond-file-fd.js.
15+
16+
const hooks = initHooks();
17+
hooks.enable();
18+
19+
const {
20+
HTTP2_HEADER_CONTENT_TYPE
21+
} = http2.constants;
22+
23+
// Use large fixture to get several file operations.
24+
const fname = fixtures.path('person-large.jpg');
25+
const fd = fs.openSync(fname, 'r');
26+
27+
const server = http2.createServer();
28+
server.on('stream', (stream) => {
29+
stream.respondWithFD(fd, {
30+
[HTTP2_HEADER_CONTENT_TYPE]: 'text/plain'
31+
});
32+
});
33+
server.on('close', common.mustCall(() => fs.closeSync(fd)));
34+
server.listen(0, () => {
35+
const client = http2.connect(`http://localhost:${server.address().port}`);
36+
const req = client.request();
37+
38+
req.on('response', common.mustCall());
39+
req.on('data', () => {});
40+
req.on('end', common.mustCall(() => {
41+
client.close();
42+
server.close();
43+
}));
44+
req.end();
45+
});
46+
47+
process.on('exit', onExit);
48+
49+
function onExit() {
50+
hooks.disable();
51+
hooks.sanityCheck();
52+
const activities = hooks.activities;
53+
54+
// Verify both invocations
55+
const fsReqs = activities.filter((x) => x.type === 'FSREQCALLBACK');
56+
assert.ok(fsReqs.length >= 2);
57+
58+
checkInvocations(fsReqs[0], { init: 1, destroy: 1 }, 'when process exits');
59+
checkInvocations(fsReqs[1], { init: 1, destroy: 1 }, 'when process exits');
60+
61+
// Verify reuse handle has been wrapped
62+
assert.ok(fsReqs[0].handle !== fsReqs[1].handle, 'Resource reused');
63+
assert.ok(fsReqs[0].handle === fsReqs[1].handle.handle,
64+
'Resource not wrapped correctly');
65+
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — 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.