Add OracleConnection.cancel() for server-side break#118
Open
iliasaz wants to merge 1 commit into
Open
Conversation
Implements OCIBreak / python-oracledb Connection.cancel() equivalent on top of the existing TNS marker plumbing. A new public `OracleConnection.cancel()` triggers a wire-level handshake that interrupts the in-flight statement on the server, so the awaiting `execute(...)` promise (or row iterator) fails with `.statementCancelled` and the connection is reusable for the next statement. Wire-level handshake mirrors python-oracledb's `_break_external` / `_reset` pair: client sends a TNS INTERRUPT marker, server replies with a BREAK acknowledgment, the existing markerReceived toggle echoes a RESET back, then the server raises ORA-01013 which the statement state machine routes through the cancellation branch (now extended to fail the awaiting promise from `.initialized`, `.describeInfoReceived`, and `.rowCountsReceived` substates). Mid-fetch cancels delegate to the existing iterator-drop path (forwardStreamError + sendMarker(RESET, read: true)) — same end state, same wire bytes. Test coverage: - 5 unit tests in `StatementStateMachineTests` covering each substate (`.initialized`, `.describeInfoReceived`, `.streaming`, idle, idempotent re-cancel). - 5 integration tests in `CancelTests` against a live database: cancelDuringKernelSleep, cancelDuringCPUBoundExecute, cancelMidFetch, cancelOnIdleConnection, doubleCancelIsIdempotent. Signed-off-by: Ilia Sazonov <ilia_saz@yahoo.com>
Owner
|
This is a great addition. What I don't like about it is that it breaks the concept of structured concurrency with the addition of the cancel method. It should work via Task.cancel instead. let task = Task {
try await conn.execute("BEGIN dbms_session.sleep(60); END;", logger: logger)
}
task.cancel()I didn't have time to review the internals yet. Do you want me to take it over? |
Author
That's a very good point! I didn't think of it. I thought of making it async but that alone doesn't get us any benefits. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a public
OracleConnection.cancel()API that asynchronously interrupts an in-flight statement on the server — the OCIOCIBreak()/ python-oracledbConnection.cancel()equivalent.Without this,
Task.cancel()on the awaited promise doesn't propagate to the wire, so the server keeps running and the nextexecute(...)queues behind it.Wire-level handshake
Mirrors python-oracledb's
_break_external+_resetpair:TNS_MARKER_TYPE_INTERRUPT.markerReceivedtoggle echoes aTNS_MARKER_TYPE_RESETback.ORA-01013on the in-flight statement.StatementStateMachine.errorReceived(_:)routes through the cancellation branch — extended in this PR to also fail the awaitingexecutepromise from the.initialized,.describeInfoReceived, and.rowCountsReceivedsubstates (the existing.streamingpath was already wired).Mid-fetch cancels delegate to the existing iterator-drop path (
.forwardStreamError(clientCancelled: true)followed bysendMarker(RESET)), so the wire bytes forawait stream.cancel()style cancellation are unchanged.Why this also benefits non-cancel call sites
The PR makes the existing
cancelStatementStreampath slightly more robust:markerStateis reset to.noMarkerSentintriggerBreak()so a cancel that follows another cancel doesn't get stuck on a stale toggle when the server elides one of the marker echoes.errorReceived(_:)now resolves the originalexecutepromise onORA-01013from any pre-streaming substate, where previously only.streamingproduced a usable failure.Code changes
Sources/OracleNIO/Connection/OracleConnection.swiftcancel()Sources/OracleNIO/OracleChannelHandler.swifttriggerBreak()entry point +.sendBreakaction handlerSources/OracleNIO/ConnectionStateMachine/ConnectionStateMachine.swifttriggerBreak()and.sendBreakcaseSources/OracleNIO/ConnectionStateMachine/StatementStateMachine.swiftmarkCancelledForBreak()returning aBreakOutcome; widenedORA-01013handlingSources/OracleNIO/Messages/Coding/OracleFrontendMessageEncoder.swiftmarker(type:)overloadSources/OracleNIO/Constants.swiftTNS_MARKER_TYPE_BREAK/_INTERRUPTtyped asUInt8for the encoderTest coverage
Unit (5 new, all 363 existing still pass)
Tests/OracleNIOTests/ConnectionStateMachine/StatementStateMachineTests.swift:breakCancellationFromInitialized— the canonicaldbms_session.sleeprepro, statement is in.initialized.breakCancellationFromDescribeInfoReceived— between describeInfo and rowHeader.breakCancellationFromStreaming— mid-stream, asserts the existingforwardStreamError+sendMarker(read: true)path is still produced.breakOnIdleConnectionIsNoOp—.readyForStatementproduces.wait.breakIsIdempotent— secondtriggerBreak()before the server responds is a no-op.Integration (5 new, against a live DB)
Tests/IntegrationTests/CancelTests.swift:cancelDuringKernelSleep—BEGIN dbms_session.sleep(10); END;, cancel after 1s; follow-upSELECTruns in well under 8s (the kernel sleep checks for breaks at finite intervals so this is order-of-seconds, not instant).cancelDuringCPUBoundExecute— cancel a CPU-boundSELECT COUNT(*)while it's executing; follow-up runs under 2s.cancelMidFetch— cancel during a 10M-row stream; iterator throws.statementCancelled, follow-up immediate.cancelOnIdleConnection— no statement in flight, follow-up unaffected.doubleCancelIsIdempotent— two cancels in quick succession leave the connection reusable.All 5 pass against the local Oracle Free 23ai test container at the standard
ORA_HOSTNAME/ORA_PORT/ORA_SERVICE_NAME/ORA_USERNAME/ORA_PASSWORDenv.Notes
scripts/soundness.shpasses (license headers, no unacceptable language,swift-formatclean).Signed-off-by:.Inspired by issue iliasaz/macintora#15 (Oracle SQL IDE) where the missing cancel was particularly visible.