You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "1996fanrui (via GitHub)" <gi...@apache.org> on 2026/09/15 10:49:15 UTC
[PR] [FLINK-40667][network] Fix buffer ref-count leak in recovery channel-state filtering [flink]
1996fanrui opened a new pull request, #29189:
URL: https://github.com/apache/flink/pull/29189
## What is the purpose of the change
During checkpointing-during-recovery, `SpillingWithFilteringHandler.recover()` retains the
pre-filter buffer at the call site, before `segmentSerializerFor()` and the
`ChannelStateFilteringHandler` dispatcher's early throws (invalid gateIndex / null gate
handler) run. Any of those throws orphans the retained reference, so its refCount never
reaches 0 and `closeInternal()` later `free()`s a `MemorySegment` still wrapped by a live
`NetworkBuffer`.
## Brief change log
- `recover()` passes the base buffer instead of `buffer.retainBuffer()`.
- The retain now happens where the dispatcher delegates to the inner `GateFilterHandler`,
whose `sourceBufferOwnershipTransferred` guard already recycles on throw.
## Verifying this change
Added `InputChannelRecoveredStateHandlerTest#testPreFilterBufferRecycledWhenFilterAndRewriteThrows`:
asserts the pre-filter buffer is recycled when `filterAndRewrite` throws. Fails before the fix,
passes after.
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): no
- The public API, i.e., is any changed class annotated with `@Public(Evolving)`: no
- The serializers: no
- The runtime per-record code paths (performance sensitive): no
- Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: yes
- The S3 file system connector: no
## Documentation
- Does this pull request introduce a new feature? no
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40667][network] Fix buffer ref-count leak in recovery channel-state filtering [flink]
Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #29189:
URL: https://github.com/apache/flink/pull/29189#issuecomment-5679156631
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "61f4d359927017b152bc7272e6f896d8ee180537",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "61f4d359927017b152bc7272e6f896d8ee180537",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 61f4d359927017b152bc7272e6f896d8ee180537 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run azure` re-run the last Azure build
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40667][network] Fix buffer ref-count leak in recovery channel-state filtering [flink]
Posted by "1996fanrui (via GitHub)" <gi...@apache.org>.
1996fanrui commented on code in PR #29189:
URL: https://github.com/apache/flink/pull/29189#discussion_r4036206014
##########
flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/channel/RecoveredChannelStateHandler.java:
##########
@@ -592,7 +592,7 @@ public void recover(
channelInfo.getGateIdx(),
oldSubtaskIndex,
channelInfo.getInputChannelIdx(),
- buffer.retainBuffer(),
+ buffer,
Review Comment:
Agreed. Ownership stays intact here — the buffer is still recycled by close() → clear(); it's only a free-vs-recycle ordering window on the already-failing teardown path, with no leak or lasting effect. Leaving it as a follow-up.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40667][network] Fix buffer ref-count leak in recovery channel-state filtering [flink]
Posted by "1996fanrui (via GitHub)" <gi...@apache.org>.
1996fanrui commented on code in PR #29189:
URL: https://github.com/apache/flink/pull/29189#discussion_r4036042720
##########
flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/channel/ChannelStateFilteringHandler.java:
##########
@@ -133,7 +133,7 @@ public void filterAndRewrite(
+ ". This gate is not a network input and should not have recovered buffers.");
}
gateHandler.filterAndRewrite(
- oldSubtaskIndex, oldChannelIndex, sourceBuffer, outputSerializer);
+ oldSubtaskIndex, oldChannelIndex, sourceBuffer.retainBuffer(), outputSerializer);
Review Comment:
Went with your symmetric alternative: hoisted `segmentSerializerFor` before the retain, and both early throws now `recycle sourceBuffer`. Both overloads consume the buffer again; the ownership test is restored.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40667][network] Fix buffer ref-count leak in recovery channel-state filtering [flink]
Posted by "1996fanrui (via GitHub)" <gi...@apache.org>.
1996fanrui merged PR #29189:
URL: https://github.com/apache/flink/pull/29189
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40667][network] Fix buffer ref-count leak in recovery channel-state filtering [flink]
Posted by "1996fanrui (via GitHub)" <gi...@apache.org>.
1996fanrui commented on code in PR #29189:
URL: https://github.com/apache/flink/pull/29189#discussion_r4036044913
##########
flink-runtime/src/test/java/org/apache/flink/runtime/checkpoint/channel/InputChannelRecoveredStateHandlerTest.java:
##########
@@ -351,6 +351,27 @@ void testPreFilterSegmentFreedOnClose() throws Exception {
assertThat(filteringHandler.getPreFilterSegmentForTesting()).isNull();
}
+ @Test
+ void testPreFilterBufferRecycledWhenFilterAndRewriteThrows() throws Exception {
Review Comment:
Switched to a non-empty array with a null entry, so the test now hits the reachable null-handler path.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40667][network] Fix buffer ref-count leak in recovery channel-state filtering [flink]
Posted by "rkhachatryan (via GitHub)" <gi...@apache.org>.
rkhachatryan commented on code in PR #29189:
URL: https://github.com/apache/flink/pull/29189#discussion_r4015931603
##########
flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/channel/ChannelStateFilteringHandler.java:
##########
@@ -133,7 +133,7 @@ public void filterAndRewrite(
+ ". This gate is not a network input and should not have recovered buffers.");
}
gateHandler.filterAndRewrite(
- oldSubtaskIndex, oldChannelIndex, sourceBuffer, outputSerializer);
+ oldSubtaskIndex, oldChannelIndex, sourceBuffer.retainBuffer(), outputSerializer);
Review Comment:
**This breaks `GateFilterHandlerBufferOwnershipTest#testCloseRecyclesDeserializerHeldBufferAfterError`** (not updated in this PR).
That test calls the *dispatcher* 5-arg overload with a buffer at refCnt 1 and asserts `sourceBuffer.isRecycled()` after the `close()` chain. With the added retain: 1 → 2; the serializer throws *after* `vc.setNextBuffer`, so `sourceBufferOwnershipTransferred` is true and `GateFilterHandler`'s catch correctly skips recycling; then `close() → clear() → deserializer.clear()` brings it 2 → 1. `NetworkBuffer.isRecycled()` is `refCnt() == 0`, so the assertion fails.
**Related: the two overloads now have opposite ownership contracts.** `GateFilterHandler.filterAndRewrite` (inner) *consumes* the caller's reference — recycles on pre-transfer throw, hands ownership to the deserializer otherwise. After this change the dispatcher *does not*; it adds its own. Same method name, same package, inverted semantics, and neither javadoc says so. The broken test above is exactly the failure mode this invites.
**Alternative that avoids the asymmetry:** keep `buffer.retainBuffer()` in `recover()`, but hoist `segmentSerializerFor(...)` into a local *before* it, and make the dispatcher's two early throws recycle `sourceBuffer` — matching the inner contract. Both overloads then behave identically and the existing ownership test stays green. If you prefer the current shape, the test needs updating and both javadocs should state the contract explicitly.
##########
flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/channel/RecoveredChannelStateHandler.java:
##########
@@ -592,7 +592,7 @@ public void recover(
channelInfo.getGateIdx(),
oldSubtaskIndex,
channelInfo.getInputChannelIdx(),
- buffer.retainBuffer(),
+ buffer,
Review Comment:
Non-blocking, and pre-existing rather than introduced here: the same `free()`-under-live-buffer hazard survives on the **post-transfer** throw path.
If `filterAndRewrite` throws after `vc.setNextBuffer` (e.g. a `DataOutputSerializer` IOException in `emitAggregated`), the deserializer still holds the reference, this `finally` goes 2 → 1, and `preFilterBufferInUse` stays `true`. In `SequentialChannelStateReaderImpl#readInputData` the *inner* try-with-resources closes `stateHandler` first, so `closeInternal()` calls `preFilterSegment.free()` while the deserializer's `NetworkBuffer` is still live; the outer `filteringHandler.close()` only clears it afterwards.
Harmless today (the task is already failing and nothing reads the freed segment), but it is the same bug class this PR closes. Either swap the close order, or have `closeInternal()` refuse to free while `preFilterBufferInUse`. Fine as a follow-up.
##########
flink-runtime/src/test/java/org/apache/flink/runtime/checkpoint/channel/InputChannelRecoveredStateHandlerTest.java:
##########
@@ -351,6 +351,27 @@ void testPreFilterSegmentFreedOnClose() throws Exception {
assertThat(filteringHandler.getPreFilterSegmentForTesting()).isNull();
}
+ @Test
+ void testPreFilterBufferRecycledWhenFilterAndRewriteThrows() throws Exception {
Review Comment:
Non-blocking: this covers the one branch that can't occur in production. The zero-length `GateFilterHandler[]` hits `gateIndex >= gateHandlers.length`, but `createFromContext` always sizes the array to `inputGates.length`, so `channelInfo.getGateIdx()` is never out of range in a real job.
The two reachable pre-retain throw sites named in the PR description are untested: `segmentSerializerFor(...)` / `getMappedChannels(...)` failing during argument evaluation, and `gateHandler == null` (reachable — `createGateHandler` returns `null` for a gate with no virtual channels). A non-empty array holding a `null` entry would exercise the actual production path.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org