fix_capture_embedder_exceptions_before_entering_v8.patch 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: deepak1556 <[email protected]>
  3. Date: Tue, 26 Dec 2023 02:10:42 +0900
  4. Subject: fix: capture embedder exceptions before entering V8
  5. Upstrem bug: https://github.com/nodejs/node-v8/issues/274
  6. The patch only addresses the callsites that triggered failing DCHECKS
  7. in the nodejs test suite. Need to be followed-up with upstream
  8. on the broader change as there maybe other callsites.
  9. diff --git a/src/handle_wrap.cc b/src/handle_wrap.cc
  10. index be02d4aaa04685cbd6a9ecfe082e38f179129ab5..277748a30bd97ae816d9ba1f2d73851a29b81010 100644
  11. --- a/src/handle_wrap.cc
  12. +++ b/src/handle_wrap.cc
  13. @@ -148,6 +148,9 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
  14. wrap->OnClose();
  15. wrap->handle_wrap_queue_.Remove();
  16. + if (env->isolate()->IsExecutionTerminating())
  17. + return;
  18. +
  19. if (!wrap->persistent().IsEmpty() &&
  20. wrap->object()->Has(env->context(), env->handle_onclose_symbol())
  21. .FromMaybe(false)) {
  22. diff --git a/src/node_contextify.cc b/src/node_contextify.cc
  23. index 8951cd378a9025f58fada47cf96f686d14639f95..6456d87d4202c013aafe071adbac06852b3ae2c1 100644
  24. --- a/src/node_contextify.cc
  25. +++ b/src/node_contextify.cc
  26. @@ -487,6 +487,7 @@ bool ContextifyContext::IsStillInitializing(const ContextifyContext* ctx) {
  27. void ContextifyContext::PropertyGetterCallback(
  28. Local<Name> property,
  29. const PropertyCallbackInfo<Value>& args) {
  30. + Environment* env = Environment::GetCurrent(args);
  31. ContextifyContext* ctx = ContextifyContext::Get(args);
  32. // Still initializing
  33. @@ -494,6 +495,8 @@ void ContextifyContext::PropertyGetterCallback(
  34. Local<Context> context = ctx->context();
  35. Local<Object> sandbox = ctx->sandbox();
  36. +
  37. + TryCatchScope try_catch(env);
  38. MaybeLocal<Value> maybe_rv =
  39. sandbox->GetRealNamedProperty(context, property);
  40. if (maybe_rv.IsEmpty()) {
  41. @@ -503,6 +506,11 @@ void ContextifyContext::PropertyGetterCallback(
  42. Local<Value> rv;
  43. if (maybe_rv.ToLocal(&rv)) {
  44. + if (try_catch.HasCaught() &&
  45. + !try_catch.HasTerminated()) {
  46. + try_catch.ReThrow();
  47. + }
  48. +
  49. if (rv == sandbox)
  50. rv = ctx->global_proxy();
  51. diff --git a/src/node_messaging.cc b/src/node_messaging.cc
  52. index e7d2bfbafef13f04a73dcbefe7d6e90b37b904d1..31b870c5f003b62b848c00d6032ed98eb829778d 100644
  53. --- a/src/node_messaging.cc
  54. +++ b/src/node_messaging.cc
  55. @@ -907,7 +907,7 @@ Maybe<bool> MessagePort::PostMessage(Environment* env,
  56. const TransferList& transfer_v) {
  57. Isolate* isolate = env->isolate();
  58. Local<Object> obj = object(isolate);
  59. -
  60. + TryCatchScope try_catch(env);
  61. std::shared_ptr<Message> msg = std::make_shared<Message>();
  62. // Per spec, we need to both check if transfer list has the source port, and
  63. @@ -915,6 +915,10 @@ Maybe<bool> MessagePort::PostMessage(Environment* env,
  64. Maybe<bool> serialization_maybe =
  65. msg->Serialize(env, context, message_v, transfer_v, obj);
  66. + if (try_catch.HasCaught() &&
  67. + !try_catch.HasTerminated()) {
  68. + try_catch.ReThrow();
  69. + }
  70. if (data_ == nullptr) {
  71. return serialization_maybe;
  72. }