Skip to content

Commit 478f1e7

Browse files
FlarnaMylesBorins
authored andcommitted
async_hooks: avoid resource reuse by FileHandle
Wrap reused read_wrap in a unique async resource to ensure that executionAsyncResource() is not ambiguous. PR-URL: #31972 Refs: #30959 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent 893e918 commit 478f1e7

4 files changed

Lines changed: 72 additions & 9 deletions

File tree

‎src/async_wrap.cc‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ AsyncWrap::AsyncWrap(Environment* env,
587587
provider_type_ = provider;
588588

589589
// Use AsyncReset() call to execute the init() callbacks.
590-
AsyncReset(execution_async_id, silent);
590+
AsyncReset(object, execution_async_id, silent);
591591
init_hook_ran_ = true;
592592
}
593593

@@ -653,10 +653,6 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
653653
env->destroy_async_id_list()->push_back(async_id);
654654
}
655655

656-
voidAsyncWrap::AsyncReset(double execution_async_id, bool silent) {
657-
AsyncReset(object(), execution_async_id, silent);
658-
}
659-
660656
// Generalized call for both the constructor and for handles that are pooled
661657
// and reused over their lifetime. This way a new uid can be assigned when
662658
// the resource is pulled out of the pool and put back into use.

‎src/async_wrap.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,6 @@ class AsyncWrap : public BaseObject {
170170
double execution_async_id = kInvalidAsyncId,
171171
bool silent = false);
172172

173-
voidAsyncReset(double execution_async_id = kInvalidAsyncId,
174-
bool silent = false);
175-
176173
// Only call these within a valid HandleScope.
177174
v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,
178175
int argc,

‎src/node_file.cc‎

Lines 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;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constinitHooks=require('./init-hooks');
5+
const{ checkInvocations }=require('./hook-checks');
6+
constfixtures=require('../common/fixtures');
7+
if(!common.hasCrypto)
8+
common.skip('missing crypto');
9+
consthttp2=require('http2');
10+
constassert=require('assert');
11+
constfs=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+
consthooks=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+
constfname=fixtures.path('person-large.jpg');
25+
constfd=fs.openSync(fname,'r');
26+
27+
constserver=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+
constclient=http2.connect(`http://localhost:${server.address().port}`);
36+
constreq=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+
functiononExit(){
50+
hooks.disable();
51+
hooks.sanityCheck();
52+
constactivities=hooks.activities;
53+
54+
// Verify both invocations
55+
constfsReqs=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)