Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix StreamPipe cleanup & interaction with env handles #26256

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,11 @@ void Environment::RegisterHandleCleanups() {
void* arg) {
handle->data = env;

env->CloseHandle(handle, [](uv_handle_t* handle) {});
env->CloseHandle(handle, [](uv_handle_t* handle) {
#ifdef DEBUG
memset(handle, 0xab, uv_handle_size(handle->type));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

#endif
});
};

RegisterHandleCleanup(
Expand Down Expand Up @@ -508,6 +512,7 @@ void Environment::PrintSyncTrace() const {
}

void Environment::RunCleanup() {
started_cleanup_ = true;
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
"RunCleanup", this);
CleanupHandles();
Expand Down Expand Up @@ -656,10 +661,13 @@ void Environment::RunAndClearNativeImmediates() {


void Environment::ScheduleTimer(int64_t duration_ms) {
if (started_cleanup_) return;
uv_timer_start(timer_handle(), RunTimers, duration_ms, 0);
}

void Environment::ToggleTimerRef(bool ref) {
if (started_cleanup_) return;

if (ref) {
uv_ref(reinterpret_cast<uv_handle_t*>(timer_handle()));
} else {
Expand Down Expand Up @@ -759,6 +767,8 @@ void Environment::CheckImmediate(uv_check_t* handle) {
}

void Environment::ToggleImmediateRef(bool ref) {
if (started_cleanup_) return;

if (ref) {
// Idle handle is needed only to stop the event loop from blocking in poll.
uv_idle_start(immediate_idle_handle(), [](uv_idle_t*){ });
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ class Environment {
CleanupHookCallback::Hash,
CleanupHookCallback::Equal> cleanup_hooks_;
uint64_t cleanup_hook_counter_ = 0;
bool started_cleanup_ = false;

static void EnvPromiseHook(v8::PromiseHookType type,
v8::Local<v8::Promise> promise,
Expand Down
2 changes: 1 addition & 1 deletion src/stream_pipe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ StreamPipe::StreamPipe(StreamBase* source,
}

StreamPipe::~StreamPipe() {
CHECK(is_closed_);
Unpipe();
}

StreamBase* StreamPipe::source() {
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-worker-terminate-http2-respond-with-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const makeDuplexPair = require('../common/duplexpair');
const { Worker, isMainThread } = require('worker_threads');

// This is a variant of test-http2-generic-streams-sendfile for checking
// that Workers can be terminated during a .respondWithFile() operation.

if (isMainThread) {
return new Worker(__filename);
}

{
const server = http2.createServer();
server.on('stream', common.mustCall((stream, headers) => {
stream.respondWithFile(process.execPath); // Use a large-ish file.
}));

const { clientSide, serverSide } = makeDuplexPair();
server.emit('connection', serverSide);

const client = http2.connect('http://localhost:80', {
createConnection: common.mustCall(() => clientSide)
});

const req = client.request();

req.on('response', common.mustCall((headers) => {
assert.strictEqual(headers[':status'], 200);
}));

req.on('data', common.mustCall(process.exit));
req.on('end', common.mustNotCall());
req.end();
}