Skip to content

fix: fail pending futures of broken items instead of leaving them pending - #4

Closed
Chris962 wants to merge 1 commit into
RelativityMC:masterfrom
Chris962:fix/fail-futures-of-broken-items
Closed

Chris962 wants to merge 1 commit into
RelativityMC:masterfrom
Chris962:fix/fail-futures-of-broken-items

Conversation

@Chris962

Copy link
Copy Markdown

I am currently working on a fork of the Async mod and found a bug when trying to use it with C2ME so i let my Agent write the PR below:


Problem

When a status upgrade fails and handleTransactionException returns MARK_BROKEN (C2ME's TheChunkSystem always does), the holder gets FLAG_BROKEN and tickHolder0 refuses any further upgrade. The futures for the statuses that are still requested are never completed, though. They only complete (with UNLOADED_EXCEPTION) once the ticket is removed and the target drops.

So anything that waits on such a future while it also keeps the ticket waits forever. In C2ME that is, for example, a blocking chunk load on the server thread: the original error is logged and reported via onChunkLoadFailure, but the getChunk caller never returns and the server ends up in the watchdog instead of failing with the real cause.

The same happens after a failed postUpgradeToThis: the forced downgrade re-creates the (still requested) future of the downgraded status, and nothing can complete it any more.

Reproduction

BrokenItemTest in this PR. Against current master, all four failure cases time out (TimeoutException after 5 s), while the success control passes:

Test master this PR
testSuccessfulUpgradeCompletesFutures pass pass
testUpgradeFailureFailsRequestedFutures hangs pass
testTicketAddedAfterBreakFails hangs pass
testFreshHolderCanRetryAfterUnload hangs pass
testPostUpgradeFailureFailsRequestedFutures hangs pass

How to reproduce

No Minecraft is needed. The scheduler alone shows it:

  1. Implement an ItemStatus whose upgradeToThis returns Completable.error(...), and a handleTransactionException that returns MARK_BROKEN (what C2ME does).
  2. addTicket(key, target, null), then wait on holder.getFutureForStatus0(target) while keeping the ticket.
  3. On master, the future never completes. The holder is broken and idle, and the error has been handed to handleTransactionException. Removing the ticket completes it with UNLOADED_EXCEPTION.

To see it with this PR's tests, keep BrokenItemTest.java and revert the two main sources to master:

git checkout master -- src/main
./gradlew test --tests '*BrokenItemTest*'

In a real server, this is reached whenever a chunk status upgrade throws (C2ME logs Error upgrading chunk ... to ...) while something still waits on that chunk, e.g. a broken worldgen mod or datapack, or a chunk that fails to load. I did not trigger it in a running server. I reproduced it with the actual FlowSched classes shipped in C2ME 0.4.2-alpha.0.106+26.3, using the same steps as above.

Fix

  • ItemHolder remembers the first failure that marked it broken (markBroken(Throwable), which replaces the direct setFlag(FLAG_BROKEN) calls in StatusAdvancingScheduler).
  • When tickHolder0 finds a broken holder whose target is above its current status, it no longer just returns. It calls failUnreachableFutures(current), which completes every still-pending future in (current, target] exceptionally with that failure, outside the holder lock (same as setStatus does).

Because this runs on every tick, it covers the initial failure, tickets added later, and the forced downgrade after a postUpgradeToThis failure. Nothing changes for holders that are not broken.

The existing future invariants still hold: futures at or below the current status are untouched, failed futures are never UNLOADED_FUTURE, and flushUnloadedStatus and the downgrade path still replace or flush them as before when the target drops. Once all tickets are gone the broken holder is removed as usual, and a new ticket creates a fresh holder that can retry (covered by testFreshHolderCanRetryAfterUnload).

Effect on C2ME

NewChunkHolderVanillaInterface.wrapOptional*Future already handles futures that fail with something other than UNLOADED_EXCEPTION: it rethrows the cause into the returned vanilla future. With this change, a waiter on a broken chunk gets the original chunk error instead of waiting forever. For a blocking getChunk that means a crash report with the real cause, rather than a watchdog kill. Once this is merged, C2ME only needs a FlowSched submodule bump.

If you'd rather waiters get UNLOADED_EXCEPTION (so vanilla sees ChunkResult "unloaded" instead of an exception), that's a one-line change in failUnreachableFutures. I went with the original cause because it's more informative and the C2ME wrappers already expect it.

Not covered

  • Dependents of a broken holder still wait. If item X needs Y at status S as a dependency and Y breaks before reaching S, X's upgrade stays parked on the dependency ticket, since ticket callbacks only fire on success. I checked this with a probe, and it behaves the same before and after this PR. Fixing it means deciding whether a failure should cascade to dependents (for chunks, potentially to every chunk around a broken one), so I left it out of this PR.
  • clearFlag(FLAG_BROKEN) followed by an upgrade would hit the !future.isDone() assertion in setStatus, because the futures were already failed. Nothing in FlowSched or C2ME clears that flag today.

Testing

  • ./gradlew test: full suite green (3 runs), and BrokenItemTest alone 20/20 runs green.
  • The new tests fail (time out) on unpatched master.
  • Ran the same scenario against the exact FlowSched bytecode shipped inside C2ME 0.4.2-alpha.0.106+26.3, with no Minecraft loaded. The future is still pending with the shipped classes. With this patch ahead of them on the classpath, it completes exceptionally with the original error. The success control is unchanged.

…ding

When an upgrade fails and handleTransactionException returns MARK_BROKEN,
the holder is no longer allowed to upgrade, but the futures of the statuses
that are still requested were left incomplete. Anyone waiting on them kept
waiting until the ticket was removed, which may never happen if the waiter
is the one holding the ticket (e.g. a blocking chunk load).

Remember the failure that broke the holder and, whenever a tick finds that
a broken holder is asked to upgrade, complete the requested futures above
the current status exceptionally with that failure. This also covers
tickets added after the holder broke and the forced downgrade after a
failed postUpgradeToThis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XCHZDR58MAQKirDihoKp1M
@ishland

ishland commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Do not submit AI slop. The current behavior is intentional and there's no plan to change this in FlowSched.

@ishland ishland closed this Oct 30, 2026
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