Skip to content

Conversation

@amccague
Copy link
Contributor

Subsystem
Jetty Jakarta Ktor Server

Motivation
https://youtrack.jetbrains.com/issue/KTOR-9290/Jetty-Jakarta-idleTimeout-does-not-cancel-the-requests-job

When Jetty's idle timeout expires during request processing, the timeout is effectively ignored if the server-side coroutine is performing slow work. Clients would still receive the intended status code beyond the idle timeout time.

The current tests are focussed on clients being slow to read and write.

The fix adds an idle timeout listener to the Jetty request that cancels the handler's coroutine job when the timeout fires therefore making it effective on server resources.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 27, 2026

Walkthrough

Adds idle-timeout-driven cancellation of the coroutine launched for request handling in the Jetty Jakarta handler and centralizes guarded error-write logic; adds a test that verifies idle timeout cancels the coroutine and unwraps the resulting CancellationException root cause.

Changes

Cohort / File(s) Summary
Handler Implementation
ktor-server/ktor-server-jetty-jakarta/jvm/src/io/ktor/server/jetty/jakarta/JettyKtorHandler.kt
Capture the coroutine job returned from application.launch; add an idle-timeout listener that cancels that job and triggers guarded error writing; refactor cancellation and general error paths to use a new private helper tryWriteError which guards response.isCommitted and uses runCatching.
Test Coverage
ktor-server/ktor-server-jetty-jakarta/jvm/test/io/ktor/tests/server/jetty/jakarta/JettyIdleTimeoutTest.kt
Enable SSL-disabled test setup; add idleTimeoutCancelsCoroutineContext() test asserting idle timeout cancels the server coroutine and that the caught CancellationException unwraps to a TimeoutException; convert mutable writeError to val; add afterTest handling to swallow expected CancellationException for that test; add CancellationException.unwrapRootCause() helper.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • bjhham
  • marychatte
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding idle timeout cancellation for Jetty Jakarta requests, directly referencing the issue KTOR-9290.
Description check ✅ Passed The description includes all required template sections with substantive content: Subsystem, Motivation (with issue link and problem description), and Solution (explaining the timeout listener fix).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

request,
response,
callback,
HttpStatus.GONE_410,
Copy link
Contributor Author

@amccague amccague Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should say I'm not a fan of this 410 Gone here - I can't see how a cancellation exception should imply

the target resource is no longer available at the origin server and that this condition is likely to be permanent

https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/410

408 probably fits better.. but in this context we can't be sure how this bubbled up so it could be a 500 like the others.

@amccague amccague force-pushed the jetty_jakarta_commit_to_idle_timeout branch from fb01cbd to 7ff857c Compare January 29, 2026 22:49
@amccague amccague force-pushed the jetty_jakarta_commit_to_idle_timeout branch from 7ff857c to 85a3e27 Compare January 29, 2026 22:51
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In
`@ktor-server/ktor-server-jetty-jakarta/jvm/test/io/ktor/tests/server/jetty/jakarta/JettyIdleTimeoutTest.kt`:
- Around line 39-41: The comment in JettyIdleTimeoutTest.kt contains a typo:
change "unhanded" to "unhandled" in the KDoc above the test (the block
mentioning afterTest override); locate the comment near the class/test that
overrides afterTest and correct the word to "unhandled" so the sentence reads
"how an unhandled CancellationException is handled" to fix the typo.
🧹 Nitpick comments (1)
ktor-server/ktor-server-jetty-jakarta/jvm/test/io/ktor/tests/server/jetty/jakarta/JettyIdleTimeoutTest.kt (1)

17-18: Redundant import: CancellationException is already covered by the star import.

Line 18 explicitly imports kotlinx.coroutines.CancellationException, but Line 17's star import kotlinx.coroutines.* already includes it.

🧹 Suggested fix
 import kotlinx.coroutines.*
-import kotlinx.coroutines.CancellationException

…erver/jetty/jakarta/JettyIdleTimeoutTest.kt

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant