Skip to content

Fix asynchronous cleanup task retries never running - #6295

Open
ayushtkn wants to merge 1 commit into
apache:mainfrom
ayushtkn:asyncTaskRetryPrincipal
Open

ayushtkn wants to merge 1 commit into
apache:mainfrom
ayushtkn:asyncTaskRetryPrincipal

Conversation

@ayushtkn

@ayushtkn ayushtkn commented Dec 2, 2026

Copy link
Copy Markdown
Member

TaskExecutorImpl is written to retry a failed task up to three times, but only
the first attempt ever ran.

Every attempt cloned the injected PolarisPrincipal inside tryHandleTask, and
a retry re-enters that method from exceptionallyComposeAsync on the
task-executor ManagedExecutor, which is built with
cleared(ThreadContext.CDI). PolarisPrincipal is produced @RequestScoped by
PolarisPrincipalHolder, so dereferencing its client proxy on that thread finds
no active request context, and the attempt died before it reached the task body.

Nothing reported it. addTaskHandlerContext discards the future it gets back, so
after a drop with purge a transient failure left the table's data, manifest and
metadata files behind, with no log line past the first attempt's warning.

handleTaskWithTracing is annotated @ActivateRequestContext and runs in a
fresh request context, so the task body itself was never affected; only
tryHandleTask sits outside that scope. This clones the principal once in
addTaskHandlerContext, next to the CallContext clone that is already there
for the same reason, and passes it to each attempt.

It also completes the discarded future with a terminal handler that logs. The
existing suppression on addTaskHandlerContext reads "it should be okay in
this particular case", and this bug is what that assumption cost: any throwable
raised before CompletableFuture.runAsync, or inside the retry composition
itself, vanished without a trace. The handler changes no behaviour, since the
future was already ignored, and it also records the outcome when a task
exhausts its attempts.

Checklist

  • 🙋 I am the human author: I understand this change and can explain it
  • 🛡️ Don't disclose security issues! (contact security@apache.org)
  • 🔗 Clearly explained why the changes are needed, or linked related issues: Fixes #
  • 🧪 Added/updated tests with good coverage, or manually tested (and explained how)
  • 💡 Added comments for complex logic
  • 🧾 Added a changelog entry in changelog.d/ (if needed, see changelog.d/README.md)
  • 📚 Updated documentation in site/content/in-dev/unreleased (if needed)
  • 🤖 Disclosed significant AI usage (recommended if AI was used, see CONTRIBUTING.md)

Copilot AI lite review requested due to automatic review settings December 2, 2026 14:03
@github-project-automation github-project-automation Bot moved this to PRs In Progress in Basic Kanban Board Dec 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A moderate active issue remains because synchronous setup failures can occur before the terminal exception handler is attached, leaving those failures without the claimed logging protection.

1 open issue
What changed in this PR

Fixes asynchronous cleanup task retries by capturing the request-scoped principal before dispatch and logging terminal task failures.

Changes:

  • Preserves the principal across retry attempts.
  • Logs exhausted task failures.
  • Adds CDI retry coverage and a changelog entry.
File Description
runtime/​service/​src/​test/​java/​org/​apache/​polaris/​service/​task/​TaskExecutorRetryCdiTest.java Adds CDI-backed coverage verifying that all retry attempts execute.
runtime/​service/​src/​main/​java/​org/​apache/​polaris/​service/​task/​TaskExecutorImpl.java Captures and reuses the principal across retries and logs terminal failures.
changelog.d/​async-task-retry-principal.md Documents the asynchronous retry principal fix.

Review effort: Lite

Comment on lines +174 to +178
tryHandleTask(taskEntityId, clone, principalClone, eventMetadata, null, 1)
.exceptionally(t -> {
LOGGER.error("Task entity id {} was not handled", taskEntityId, t);
return null;
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

PolarisCallContext.copy() resolves the realm identifier eagerly and returns () -> realmId, so the getRealmContext().getRealmIdentifier() call in tryHandleTask reads off that snapshot and cannot fail.

The copy and the principal clone both happen in addTaskHandlerContext before the handler is attached, so a throw there propagates to the caller rather than being dropped — that is already the loud path. The handler covers the path that was silent: a retry re-enters tryHandleTask from inside exceptionallyComposeAsync, where a synchronous throw completes the composed future exceptionally, and that future is the one that was discarded.

@orca-review-bot orca-review-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice catch. The diagnosis checks out: retries re-enter tryHandleTask on the CDI-cleared task-executor, so dereferencing the request-scoped principal proxy there was failing before the attempt began. Cloning it once next to callContext.copy() is the right minimal fix. The new CDI test fails without the fix, since an untyped task throws a retryable TaskHandlerNotFoundException and attempt 3 is only reached once retries work. spotlessJavaCheck passes locally. Two small things outside the diff: can you tick the checklist that applies and link an issue, or say there isn't one? And can the test drop its task entity at the end so it doesn't leak into the shared store?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

tryHandleTask(taskEntityId, clone, eventMetadata, null, 1);
tryHandleTask(taskEntityId, clone, principalClone, eventMetadata, null, 1)
.exceptionally(t -> {
LOGGER.error("Task entity id {} was not handled", taskEntityId, t);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When a task runs out of attempts, or hits a FileDeletionTimeoutException, the same throwable has already been logged at WARN in tryHandleTask. This ERROR then prints the full stack a second time. Can we drop the t here, or make it a one-line 'gave up on task {}' message?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The duplicate is real, but the handler exists for throwables that have no other log line: a synchronous throw inside the retry composition reaches it with nothing logged, which is the failure this PR is about. Dropping t would lose the stack in exactly that case, so I'd keep it.

private @NonNull CompletableFuture<Void> tryHandleTask(
long taskEntityId,
CallContext callContext,
PolarisPrincipal principal,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Retries call this on a thread with CDI context cleared, and that's how the bug got in. Can we add a one-line comment saying tryHandleTask must only use what it's passed, and never polarisPrincipal or other request-scoped fields?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

added

callContext.getPolarisCallContext(), null, taskEntity);
assertThat(created.isSuccess())
.describedAs("task entity create: %s", created.getReturnStatus())
.isTrue();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Once all three attempts fail, this task entity stays persisted in the shared DefaultProfile store. Can we drop it at the end of the test so later tests in the same Quarkus app don't find a stray task?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done, the test now drops the entity in a finally so it goes even if the assertion fails.

TaskExecutorImpl retries a failed task up to three times, but only the first
attempt ever ran. Every attempt cloned the injected PolarisPrincipal inside
tryHandleTask, and a retry re-enters that method from exceptionallyComposeAsync
on the task-executor ManagedExecutor, which is built with
cleared(ThreadContext.CDI). PolarisPrincipal is produced @RequestScoped by
PolarisPrincipalHolder, so dereferencing its client proxy there finds no active
request context and the attempt died before reaching the task body.

Nothing reported it. addTaskHandlerContext discards the returned future, so a
dropped table's data, manifest and metadata files were left behind with no log
line past the first attempt's warning.

handleTaskWithTracing is annotated @ActivateRequestContext and runs in a fresh
request context, so the task body itself was never affected; only tryHandleTask
sits outside that scope. Clone the principal once in addTaskHandlerContext,
next to the CallContext clone that exists for the same reason, and pass it to
each attempt.

Also complete the discarded future with a terminal handler that logs. The
suppression on addTaskHandlerContext reads "it _should_ be okay in this
particular case", and this bug is what that assumption cost: any throwable
raised before CompletableFuture.runAsync, or inside the retry composition
itself, vanished without a trace. The handler changes no behaviour, since the
future was already ignored, and it also records the outcome when a task
exhausts its attempts.

TaskExecutorImplTest constructs the task executor directly, so the principal it
holds is an ordinary instance rather than a client proxy and its retry test
cannot reach this path; that test also asserts only that at least one attempt
ran. The new test drives the real CDI container and asserts the last attempt
reported is the third.
@ayushtkn
ayushtkn force-pushed the asyncTaskRetryPrincipal branch from 8954e23 to 3409b38 Compare December 3, 2026 02:07

@dimas-b dimas-b left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice catch, @ayushtkn 👍 Thanks a lot!

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Dec 3, 2026

This branch has not been deployed

No deployments
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.

4 participants