Skip to content

Hide CloseToken frames from stack traces#5590

Merged
osipxd merged 1 commit into
mainfrom
osipxd/closetoken-stacktrace
May 11, 2026
Merged

Hide CloseToken frames from stack traces#5590
osipxd merged 1 commit into
mainfrom
osipxd/closetoken-stacktrace

Conversation

@osipxd
Copy link
Copy Markdown
Member

@osipxd osipxd commented May 8, 2026

Subsystem
ktor-io

Motivation
wrapCause and throwOrNull add some noise to stacktraces

Solution
Make them inline, so they don't add any trace frames.

Before:

io.ktor.utils.io.ClosedByteChannelException: Socket timeout has expired [url=http://localhost:60230/sse/delay/2000, socket_timeout=1000] ms
	at io.ktor.utils.io.CloseToken$wrapCause$1.invoke(CloseToken.kt:17)
	at io.ktor.utils.io.CloseToken$wrapCause$1.invoke(CloseToken.kt:17)
	at io.ktor.utils.io.CloseToken.wrapCause(CloseToken.kt:23)
	at io.ktor.utils.io.CloseToken.wrapCause$default(CloseToken.kt:16)
	at io.ktor.utils.io.ByteChannel.cancel(ByteChannel.kt:145)
	at io.ktor.utils.io.ByteWriteChannelOperationsKt.close(ByteWriteChannelOperations.kt:130)
	at io.ktor.utils.io.ByteReadChannelOperationsKt.copyTo(ByteReadChannelOperations.kt:213)
	at io.ktor.utils.io.ByteReadChannelOperationsKt$copyTo$2.invokeSuspend(ByteReadChannelOperations.kt)
	at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:42)
	at io.ktor.utils.io.ByteChannel.awaitContent(ByteChannel.kt:307)
	at io.ktor.utils.io.ByteReadChannelOperationsKt.internalReadLineTo(ByteReadChannelOperations.kt:686)
	at io.ktor.utils.io.ByteReadChannelOperationsKt.readLineStrict(ByteReadChannelOperations.kt:638)
	at io.ktor.client.engine.apache5.Apache5HttpClientTest$testSocketTimeoutWithCustomConnectionManager$1$1$1.invokeSuspend(Apache5HttpClientTest.kt:44)
	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:77)
	at io.ktor.client.engine.apache5.Apache5HttpClientTest$testSocketTimeoutWithCustomConnectionManager$1.invokeSuspend(Apache5HttpClientTest.kt:41)

After:

io.ktor.utils.io.ClosedByteChannelException: Socket timeout has expired [url=http://localhost:58759/sse/delay/2000, socket_timeout=1000] ms
	at io.ktor.utils.io.ByteChannel.cancel(ByteChannel.kt:446)
	at io.ktor.utils.io.ByteWriteChannelOperationsKt.close(ByteWriteChannelOperations.kt:130)
	at io.ktor.utils.io.ByteReadChannelOperationsKt.copyTo(ByteReadChannelOperations.kt:213)
	at io.ktor.utils.io.ByteReadChannelOperationsKt$copyTo$2.invokeSuspend(ByteReadChannelOperations.kt)
	at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:42)
	at io.ktor.utils.io.ByteChannel.awaitContent(ByteChannel.kt:307)
	at io.ktor.utils.io.ByteReadChannelOperationsKt.internalReadLineTo(ByteReadChannelOperations.kt:686)
	at io.ktor.utils.io.ByteReadChannelOperationsKt.readLineStrict(ByteReadChannelOperations.kt:638)
	at io.ktor.client.engine.apache5.Apache5HttpClientTest$testSocketTimeoutWithCustomConnectionManager$1$1$1.invokeSuspend(Apache5HttpClientTest.kt:44)
	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:77)
	at io.ktor.client.engine.apache5.Apache5HttpClientTest$testSocketTimeoutWithCustomConnectionManager$1.invokeSuspend(Apache5HttpClientTest.kt:41)

@osipxd osipxd self-assigned this May 8, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 8, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 78c6473f-1a4e-42aa-ad23-b5d2cf13694e

📥 Commits

Reviewing files that changed from the base of the PR and between 6d662e1 and 9cb2dfb.

📒 Files selected for processing (5)
  • ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt
  • ktor-io/common/src/io/ktor/utils/io/CloseToken.kt
  • ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt
  • ktor-io/common/src/io/ktor/utils/io/SourceByteReadChannel.kt
  • ktor-io/jvm/src/io/ktor/utils/io/jvm/javaio/Reading.kt

📝 Walkthrough

Walkthrough

This PR refactors CloseToken's wrapCause and throwOrNull from instance methods to inline extension functions within the companion object, then adds required imports across five source files to support existing cause-wrapping logic in channel implementations.

Changes

CloseToken Extension Refactoring

Layer / File(s) Summary
Core Extension Functions
ktor-io/common/src/io/ktor/utils/io/CloseToken.kt
wrapCause and throwOrNull move from instance methods to inline CloseToken extensions in companion object, preserving conditional handling of null, CopyableThrowable, CancellationException, and other throwables.
Common Channel Imports
ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt, ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt, ktor-io/common/src/io/ktor/utils/io/SourceByteReadChannel.kt
Imports for CloseToken.Companion.wrapCause and CloseToken.Companion.throwOrNull are added to support existing closedCause and error-translation accessors.
JVM-Specific Imports
ktor-io/jvm/src/io/ktor/utils/io/jvm/javaio/Reading.kt
Import for CloseToken.wrapCause is added to support RawSourceChannel.closedCause accessor.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • ktorio/ktor#5575: Related refactoring that converts CloseToken.wrapCause/throwOrNull into companion receiver extensions and updates imports for channel close cause wrapping and propagation.

Suggested reviewers

  • e5l
  • bjhham
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Hide CloseToken frames from stack traces' accurately describes the main objective of the PR—refactoring wrapCause and throwOrNull functions to be inline to eliminate stack-trace noise.
Description check ✅ Passed The PR description includes all required template sections: Subsystem (ktor-io), Motivation (wrapCause/throwOrNull add noise), and Solution (make them inline). It also provides concrete before/after stack-trace examples demonstrating the improvement.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch osipxd/closetoken-stacktrace

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.

@osipxd osipxd marked this pull request as ready for review May 8, 2026 12:26
@osipxd osipxd requested a review from bjhham May 8, 2026 12:27
@osipxd osipxd enabled auto-merge (squash) May 8, 2026 15:17
Copy link
Copy Markdown
Contributor

@bjhham bjhham left a comment

Choose a reason for hiding this comment

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

Good idea, the traces are much cleaner!

@osipxd osipxd merged commit ccb08e9 into main May 11, 2026
15 of 17 checks passed
@osipxd osipxd deleted the osipxd/closetoken-stacktrace branch May 11, 2026 11:55
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.

2 participants