fix!(server): #575 Rethrow CancellationException in StdioServerTransport, fix ReadBuffer - #571
Conversation
7c2669c to
91b2d9a
Compare
18efbcd to
9e2cf25
Compare
devcrocod
left a comment
There was a problem hiding this comment.
lgtm
I’ve left a few comments, but they’re minor and mostly related to code style
b16cbab to
aef2c76
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
Thank you, @devcrocod, for your comments! I tried to address most of them, please have a look |
1d05801 to
61f8392
Compare
|
|
||
| readChannel.close() | ||
|
|
||
| processingJob?.cancelAndJoin() |
e5l
left a comment
There was a problem hiding this comment.
please check the comments before merging
| @Suppress("TooGenericExceptionCaught") | ||
| val message = try { | ||
| deserializeMessage(line) | ||
| } catch (e: Exception) { |
There was a problem hiding this comment.
| } catch (e: Exception) { | |
| } catch (e: Throwable) { |
| } catch (e: Throwable) { | ||
| } catch (e: CancellationException) { | ||
| throw e | ||
| } catch (e: Exception) { |
There was a problem hiding this comment.
| } catch (e: Exception) { | |
| } catch (e: Throwable) { |
| val message = try { | ||
| readBuffer.readMessage() | ||
| } catch (e: Throwable) { | ||
| } catch (e: Exception) { |
There was a problem hiding this comment.
Could you tell me why we want to skip logging Error? https://docs.oracle.com/javase/8/docs/api/java/lang/Error.html
There was a problem hiding this comment.
Things like OutOfMemoryError are not actionable and usually not handled by the application and propagated.
There was a problem hiding this comment.
they still needs to be logged by the configured logger
There was a problem hiding this comment.
Logging is never guaranteed in the situation when JVM is crashing.
E.g., an OutOfMemoryError usually means the JVM has insufficient memory to allocate additional objects. Application loggers often allocate new objects (e.g., strings, log event objects, buffers) or interact with subsystems (e.g., disk I/O, network I/O). These operations may fail or behave unpredictably under memory pressure.
Logging to System.err might make sense, but is still questionable.
…ServerTransport - Replace generic `Throwable` catches with specific `CancellationException` and `Exception` for clarity and correctness. **breaking change** `Throwable` is not handled any more! - Refactor launching jobs to separate methods - Introduce `READ_BUFFER_SIZE` constant to replace inline buffer size literals. - Add suppress annotations for clearer intent and constructor documentation for better usability. - Extend StdioServerTransportTest
Previously, readMessage() returned null after consuming an unparseable line even when more complete lines existed in the buffer. This caused valid messages following a bad line in the same chunk to be silently dropped until the next chunk arrived (or forever, in tests).
Fix the loop so null means only "no complete line available", not "encountered a parse failure". Blank/whitespace-only lines are now silently skipped via isBlank() rather than forwarded to the deserializer and logged as errors.
Refactor the method into three focused helpers:
- readMessage() — outer loop over lines
- readLine() — consume the next newline-delimited line from the buffer
- tryRecover() — attempt deserialization from the first '{' onward
- Add test verifying server continues processing messages despite handler exceptions. - Introduce `ParameterizedTest` for various exception types in message handler. - Ensure `onError` is not invoked for `CancellationException`.
…dioServerTransport tests - Introduced `runIntegrationTest` utility for consistent test execution with timeout configuration. - Adjusted test utils module in Gradle build to include new helper. - Simplified exception handling and concurrency in StdioServerTransportTest.
…verTransport - Refactored job management to include explicit tracking for the processing job. Enhanced resource cleanup logic. - Updated constructor parameters KDoc to source/sink to match actual types. Renamed internal variable `outputWriter` to `outputSink` to reflect updated abstraction. - Added helper method `logJobCompletion` for consistent logging of job lifecycle events.
61f8392 to
9d47a71
Compare
…t and tests Addressed PR comments: - Updated exception handling in `StdioServerTransport` to catch `Throwable` instead of `Exception`. - Adjusted test methods and helper functions to handle `Throwable`.
|
@e5l, I’ve reverted to using Throwables. Could you take a look? |
Rethrow CancellationException in StdioServerTransport, fix ReadBuffer
Changes
StdioServerTransport
Throwablecatches with specificCancellationExceptionandExceptionfor clarity and correctness. breaking changeThrowableis not handled any more!READ_BUFFER_SIZEconstant to replace inline buffer size literals.ReadBuffer
Previously, readMessage() returned null after consuming an unparseable line even when more complete lines existed in the buffer. This caused valid messages following a bad line in the same chunk to be silently dropped until the next chunk arrived (or forever, in tests).
Fix the loop so null means only "no complete line available", not "encountered a parse failure". Blank/whitespace-only lines are now silently skipped via isBlank() rather than forwarded to the deserializer and logged as errors.
Refactor the method into three focused helpers:
Add utility method
runIntegrationTestfor integration testing (runBlocking+withTimeout). Use it in StdioServerTransportTestStore processing Job reference and
calcelAndJoin()it on close. Partially addressing StdioServerTransport: The coroutine scope that's used in this transport is completely detached from the coroutine tree #574Motivation and Context
See #575, #564, #242
How Has This Been Tested?
Unit test added
Breaking Changes
Semantic change:
Throwableand CancellationException are not handled any more!Types of changes
Checklist
Additional context