diff --git a/src/workerd/io/limit-enforcer.h b/src/workerd/io/limit-enforcer.h index c474a3484c1..ee05f74c509 100644 --- a/src/workerd/io/limit-enforcer.h +++ b/src/workerd/io/limit-enforcer.h @@ -108,6 +108,14 @@ class IsolateLimitEnforcer: public kj::Refcounted { return 128 * 1024 * 1024; // 128 MB } + // How long a Durable Object storage operation may hold the output gate before the gate breaks + // and the object is reset. This only protects against total hangs. Lowering it much in + // production is risky, since a low timeout can fire spuriously under heavy CPU load and fail + // requests that would otherwise succeed; tests lower it to exercise the timeout quickly. + virtual kj::Duration getOutputGateTimeout() const { + return 30 * kj::SECONDS; + } + // IoContext::awaitJs() calls this when the promise it waits for is never settled, which can be // in a GC finalizer or without the isolate lock, so it must neither enter V8 nor need the lock. virtual bool hasExcessivelyExceededHeapLimit() const = 0; diff --git a/src/workerd/io/worker.c++ b/src/workerd/io/worker.c++ index 3225b721737..a1a1e5634a2 100644 --- a/src/workerd/io/worker.c++ +++ b/src/workerd/io/worker.c++ @@ -4199,10 +4199,14 @@ struct Worker::Actor::Impl { public ActorCache::Hooks, private kj::TaskSet::ErrorHandler { public: - HooksImpl(kj::Own loopback, TimerChannel& timerChannel, ActorObserver& metrics) + HooksImpl(kj::Own loopback, + TimerChannel& timerChannel, + ActorObserver& metrics, + kj::Duration outputGateTimeout) : loopback(kj::mv(loopback)), timerChannel(timerChannel), - metrics(metrics) {} + metrics(metrics), + outputGateTimeout(outputGateTimeout) {} void inputGateLocked() override { metrics.inputGateLocked(); @@ -4219,11 +4223,8 @@ struct Worker::Actor::Impl { // Implements InputGate::Hooks. kj::Promise makeTimeoutPromise() override { - // This really only protects against total hangs. Lowering the timeout drastically is risky, - // since low timeouts can spuriously fire when under heavy CPU load, failing requests that - // would otherwise succeed. - auto timeout = 30 * kj::SECONDS; - co_await timerChannel.afterLimitTimeout(timeout); + // See IsolateLimitEnforcer::getOutputGateTimeout() for why this is long. + co_await timerChannel.afterLimitTimeout(outputGateTimeout); auto e = KJ_EXCEPTION(OVERLOADED, "broken.outputGateBroken; jsg.Error: Durable Object storage operation exceeded " @@ -4265,6 +4266,7 @@ struct Worker::Actor::Impl { kj::Own loopback; // only for updateAlarmInMemory() TimerChannel& timerChannel; // only for afterLimitTimeout() and updateAlarmInMemory() ActorObserver& metrics; + kj::Duration outputGateTimeout; kj::Maybe> maybeAlarmPreviewTask; @@ -4367,7 +4369,10 @@ struct Worker::Actor::Impl { container(kj::mv(container)), containerImages(kj::mv(containerImages)), facetManager(facetManager), - hooks(loopback->addRef(), timerChannel, *metrics), + hooks(loopback->addRef(), + timerChannel, + *metrics, + self.worker->getIsolate().getLimitEnforcer().getOutputGateTimeout()), inputGate(hooks), outputGate(hooks), loopback(kj::mv(loopback)),