Skip to content

Commit 583a868

Browse files
committed
stream_wrap: add HandleScope's in uv callbacks
Ensure that no handles will leak into global HandleScope by adding HandleScope's in all JS-calling libuv callbacks in `stream_wrap.cc`. Fix: #1075 PR-URL: #1078 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent b27931b commit 583a868

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/stream_wrap.cc

+8
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ void StreamWrap::OnAlloc(uv_handle_t* handle,
145145
size_t suggested_size,
146146
uv_buf_t* buf) {
147147
StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
148+
HandleScope scope(wrap->env()->isolate());
149+
Context::Scope context_scope(wrap->env()->context());
150+
148151
CHECK_EQ(wrap->stream(), reinterpret_cast<uv_stream_t*>(handle));
149152

150153
return static_cast<StreamBase*>(wrap)->OnAlloc(suggested_size, buf);
@@ -229,6 +232,7 @@ void StreamWrap::OnReadCommon(uv_stream_t* handle,
229232
const uv_buf_t* buf,
230233
uv_handle_type pending) {
231234
StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
235+
HandleScope scope(wrap->env()->isolate());
232236

233237
// We should not be getting this callback if someone as already called
234238
// uv_close() on the handle.
@@ -280,6 +284,8 @@ int StreamWrap::DoShutdown(ShutdownWrap* req_wrap) {
280284

281285
void StreamWrap::AfterShutdown(uv_shutdown_t* req, int status) {
282286
ShutdownWrap* req_wrap = ContainerOf(&ShutdownWrap::req_, req);
287+
HandleScope scope(req_wrap->env()->isolate());
288+
Context::Scope context_scope(req_wrap->env()->context());
283289
req_wrap->Done(status);
284290
}
285291

@@ -354,6 +360,8 @@ int StreamWrap::DoWrite(WriteWrap* w,
354360

355361
void StreamWrap::AfterWrite(uv_write_t* req, int status) {
356362
WriteWrap* req_wrap = ContainerOf(&WriteWrap::req_, req);
363+
HandleScope scope(req_wrap->env()->isolate());
364+
Context::Scope context_scope(req_wrap->env()->context());
357365
req_wrap->Done(status);
358366
}
359367

src/tls_wrap.cc

-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ using v8::Function;
2626
using v8::FunctionCallbackInfo;
2727
using v8::FunctionTemplate;
2828
using v8::Handle;
29-
using v8::HandleScope;
3029
using v8::Integer;
3130
using v8::Local;
3231
using v8::Null;
@@ -251,8 +250,6 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
251250
SSL* ssl = const_cast<SSL*>(ssl_);
252251
TLSWrap* c = static_cast<TLSWrap*>(SSL_get_app_data(ssl));
253252
Environment* env = c->env();
254-
HandleScope handle_scope(env->isolate());
255-
Context::Scope context_scope(env->context());
256253
Local<Object> object = c->object();
257254

258255
if (where & SSL_CB_HANDSHAKE_START) {
@@ -395,9 +392,6 @@ void TLSWrap::ClearOut() {
395392
if (eof_)
396393
return;
397394

398-
HandleScope handle_scope(env()->isolate());
399-
Context::Scope context_scope(env()->context());
400-
401395
CHECK_NE(ssl_, nullptr);
402396

403397
char out[kClearOutChunkSize];
@@ -470,9 +464,6 @@ bool TLSWrap::ClearIn() {
470464
return true;
471465
}
472466

473-
HandleScope handle_scope(env()->isolate());
474-
Context::Scope context_scope(env()->context());
475-
476467
// Error or partial write
477468
int err;
478469
Local<Value> arg = GetSSLError(written, &err, &error_);
@@ -588,8 +579,6 @@ int TLSWrap::DoWrite(WriteWrap* w,
588579

589580
if (i != count) {
590581
int err;
591-
HandleScope handle_scope(env()->isolate());
592-
Context::Scope context_scope(env()->context());
593582
Local<Value> arg = GetSSLError(written, &err, &error_);
594583
if (!arg.IsEmpty())
595584
return UV_EPROTO;
@@ -662,8 +651,6 @@ void TLSWrap::DoRead(ssize_t nread,
662651
eof_ = true;
663652
}
664653

665-
HandleScope handle_scope(env()->isolate());
666-
Context::Scope context_scope(env()->context());
667654
OnRead(nread, nullptr);
668655
return;
669656
}
@@ -796,7 +783,6 @@ int TLSWrap::SelectSNIContextCallback(SSL* s, int* ad, void* arg) {
796783
if (servername == nullptr)
797784
return SSL_TLSEXT_ERR_OK;
798785

799-
HandleScope scope(env->isolate());
800786
// Call the SNI callback and use its return value as context
801787
Local<Object> object = p->object();
802788
Local<Value> ctx = object->Get(env->sni_context_string());

0 commit comments

Comments
 (0)