Skip to content
Merged
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
60 changes: 60 additions & 0 deletions src/workerd/io/incoming-request-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,66 @@ KJ_TEST("IoContext::IncomingRequest::drain() releases a superseded (non-front) r
fixture.drainAndDestroy(kj::mv(second));
}

// An IoChannelFactory numbered in the order the fixture's incoming requests were created.
struct NumberedChannelFactory final: public TestFixture::DummyIoChannelFactory {
NumberedChannelFactory(TimerChannel& timer, uint number)
: DummyIoChannelFactory(timer),
number(number) {}
uint number;
};

uint channelFactoryNumber(IoContext& context) {
return kj::downcast<NumberedChannelFactory>(context.getIoChannelFactory()).number;
}

// Delivers two requests to one actor, the older numbered 0 and the newer 1, and has the newer
// return while the actor has background work, so that it stays current. Calls `body` with the
// actor's context and a function that has the older request return too.
void withReturnedNewerRequest(TestFixture::SetupParams params,
kj::FunctionParam<void(IoContext&, kj::FunctionParam<void()> returnOlder)> body) {
uint created = 0;
params.actorId = Worker::Actor::Id(kj::str("io-channels-test"));
params.ioChannelFactory = kj::Function<kj::Rc<IoChannelFactory>(TimerChannel&)>(
[&created](TimerChannel& timer) -> kj::Rc<IoChannelFactory> {
return kj::rc<NumberedChannelFactory>(timer, created++);
});
TestFixture fixture(kj::mv(params));
auto context = fixture.newIoContext();
auto older = fixture.newIncomingRequest(*context);
auto newer = fixture.newIncomingRequest(*context);
context->addWaitUntil(kj::NEVER_DONE);

ErrorHandlerImpl errorHandler;
kj::TaskSet waitUntilTasks(errorHandler);
newer->drain(waitUntilTasks, kj::mv(newer));

auto returnOlder = [&]() {
if (older.get() != nullptr) older->drain(waitUntilTasks, kj::mv(older));
};
body(*context, returnOlder);
returnOlder();
}

KJ_TEST("an actor's calls use the channels of its newest running request") {
withReturnedNewerRequest(
{.autogateKeys = kj::arr(util::AutogateKey::IO_CHANNELS_FROM_RUNNING_REQUEST)},
[](IoContext& context, kj::FunctionParam<void()> returnOlder) {
KJ_EXPECT(channelFactoryNumber(context) == 0);

// Once every request has returned, the calls use the current request's channels.
returnOlder();
KJ_EXPECT(channelFactoryNumber(context) == 1);
});
}

KJ_TEST("without IO_CHANNELS_FROM_RUNNING_REQUEST, an actor's calls use its current request's "
"channels") {
withReturnedNewerRequest(
{.ignoreAllAutogatesEnv = true}, [](IoContext& context, kj::FunctionParam<void()>) {
KJ_EXPECT(channelFactoryNumber(context) == 1);
});
}

KJ_TEST("cancelPendingWorkForHibernation() cancels pending work but keeps abort watchers armed") {
TestFixture fixture({.actorId = Worker::Actor::Id(kj::str("hibernate-test"))});
auto& waitScope = fixture.getWaitScope();
Expand Down
13 changes: 13 additions & 0 deletions src/workerd/io/io-context.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,19 @@ jsg::Value IoContext::makeUserTraceScopeValue(
return js.v8Ref(contextHandle);
}

IoContext::IncomingRequest& IoContext::getIncomingRequestForIoChannels() {
Comment thread
bsobel-cf marked this conversation as resolved.
Resolved
auto& current = getCurrentIncomingRequest();
if (!util::Autogate::isEnabled(util::AutogateKey::IO_CHANNELS_FROM_RUNNING_REQUEST)) {
return current;
}
for (auto& request: incomingRequests) {
if (!request.waitedForWaitUntil) {
return request;
}
}
return current;
}

SpanParent IoContext::getCurrentTraceSpan() {
// If called while lock is held, try to use the trace info stored in the async context.
KJ_IF_SOME(lock, currentLock) {
Expand Down
26 changes: 18 additions & 8 deletions src/workerd/io/io-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,9 @@ class IoContext_IncomingRequest final: public kj::PtrTarget {

kj::UnwindDetector unwindDetector;

// Used for debugging, tracks whether we properly called drain() or some other mechanism to
// wait for waitUntil tasks.
// Tracks whether we called drain() or some other mechanism to wait for waitUntil tasks, which
// happens once the request has returned, or is canceled for hibernation. Used for debugging, and
// by IoContext::getIncomingRequestForIoChannels() to tell whether the request is still running.
bool waitedForWaitUntil = false;

// If drain() was already called, this is non-null and fulfilling it will cancel the drain.
Expand Down Expand Up @@ -1379,13 +1380,14 @@ class IoContext final: public kj::Refcounted, private kj::TaskSet::ErrorHandler

jsg::JsObject getPromiseContextTag(jsg::Lock& js);

// The IoChannelFactory must be accessed through the
// currentIncomingRequest because it has some tracing context built in.
// The IoChannelFactory must be accessed through an IncomingRequest because it has some tracing
// context built in, and the embedder may stamp outgoing calls with the request's metadata, such
// as a loop-detection TTL. getIncomingRequestForIoChannels() says which request that is.
//
// TODO(later): this is made public for Python Workers. It should be possible to make this private
// again later.
IoChannelFactory& getIoChannelFactory() {
return *getCurrentIncomingRequest().ioChannelFactory;
return *getIncomingRequestForIoChannels().ioChannelFactory;
}

void pumpMessageLoop();
Expand Down Expand Up @@ -1624,15 +1626,23 @@ class IoContext final: public kj::Refcounted, private kj::TaskSet::ErrorHandler
// usage on the part of the actor, including outgoing subrequests, is attributed to the current
// request for logging and tracing. This is a hack, we don't actually know which request
// "caused" any particular resource usage, so this is merely our best guess.
//
// The IoChannelFactory must also be accessed through the currentIncomingRequest because it has
// some tracing context built in.
IncomingRequest& getCurrentIncomingRequest() {
KJ_REQUIRE(!incomingRequests.empty(), "the IoContext has no current IncomingRequest",
lastDeliveredLocation);
return incomingRequests.front();
}

// The request whose IoChannelFactory getIoChannelFactory() returns. Without the
// IO_CHANNELS_FROM_RUNNING_REQUEST autogate, this is the current request. With it, it is the
// newest request that is still running, or the current request if every request has returned.
//
// An actor's newest request stays current after it returns, while it waits for background work.
// So without the autogate, a request that returned long ago can keep governing the calls that
// other, still-running requests make, such as calls through a long-lived JS RPC session. With it,
// work that a request leaves running after it returns makes its calls through the newest running
// request too, since we don't know which request started that work.
IncomingRequest& getIncomingRequestForIoChannels();

// Run the given callback within the scope of this IoContext. This encapsulates the
// setup of a number of scopes that must be entered prior to running within the
// context, including entering the V8StackScope and acquiring the Worker::Lock.
Expand Down
6 changes: 5 additions & 1 deletion src/workerd/util/autogate.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ namespace workerd::util {
/* Worker::Isolate's constructor skips its MemoryPressureNotification(kModerate) call, which \
flushes the Liftoff code of every Wasm module in the process and empties the isolate \
group's memory pools. */ \
V(SKIP_ISOLATE_MEMORY_PRESSURE_HINT)
V(SKIP_ISOLATE_MEMORY_PRESSURE_HINT) \
/* An actor's outgoing calls use the I/O channels of its newest request that is still running, \
rather than its newest request, which may have returned and be waiting only for background \
work. See IoContext::getIncomingRequestForIoChannels(). */ \
V(IO_CHANNELS_FROM_RUNNING_REQUEST)
// clang-format on
// --------------------------------------------------------------------------------------

Expand Down
Loading