Skip to content

Commit

Permalink
Handle synchronous JS exceptions in PumpToReader
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed May 24, 2023
1 parent 02516d9 commit 1d2ff71
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/workerd/api/streams/standard.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2869,8 +2869,19 @@ public:
// that the PumpToReader, and the sink it owns, are always accessed from the right
// IoContext. Thw WeakRef ensures that if the PumpToReader is freed while
// the JS continuation is pending, there won't be a dangling reference.
return ioContext.awaitJs(
pumpLoop(js, ioContext, kj::mv(readable), ioContext.addObject(addWeakRef())));
//
// The call to awaitJs here can throw a JsExceptionThrown error synchronously
// if, for instance, the isolate is being terminated due to an exception.
// We wrap this in a js.tryCatch to catch that case and return a rejected
// kj::Promise instead.
return js.tryCatch([&] {
return ioContext.awaitJs(
pumpLoop(js, ioContext, kj::mv(readable), ioContext.addObject(addWeakRef())));
}, [&](jsg::Value exception) {
auto handle = exception.getHandle(js);
KJ_ASSERT(!handle.IsEmpty());
return js.exceptionToKj(kj::mv(exception));
});
}
KJ_CASE_ONEOF(pumping, Pumping) {
return KJ_EXCEPTION(FAILED, "pumping is already in progress");
Expand Down Expand Up @@ -3110,10 +3121,8 @@ private:
}, [&](jsg::Value exception) {
// Exceptions here should be rare, and fairly odd. We would get here,
// for instance, if the call to .Then inside read().then(...) failed.
// We need to understand more about this case so we're going to temporarily
// add logging to see if we can catch it in the act.
KJ_LOG(ERROR, "Unexpected exception in PumpToReader pump loop: ",
exception.getHandle(js));
auto handle = exception.getHandle(js);
KJ_ASSERT(!handle.IsEmpty());
return js.rejectedPromise<void>(kj::mv(exception));
});
}
Expand Down

0 comments on commit 1d2ff71

Please sign in to comment.