Skip to content

fix: Register background search tasks as children of parent - #5888

Merged
Swiddis merged 1 commit into
opensearch-project:mainfrom
Swiddis:fix/5647-zombie-tasks
Oct 19, 2026
Merged

Swiddis merged 1 commit into
opensearch-project:mainfrom
Swiddis:fix/5647-zombie-tasks

Conversation

@Swiddis

@Swiddis Swiddis commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

Description

OpenSearch automatically propagates task cancellation to any child tasks, but we weren't actually marking our search tasks as children of the background search process. This led to zombie tasks being left behind on query cancellation.

Related Issues

Resolves #5647

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@github-actions

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🧪 PR contains tests
🔒 No security concerns identified
✅ No TODO sections
🔀 No multiple PR themes
⚡ No major issues detected

@Swiddis Swiddis added the bugFix label Oct 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Remove redundant boolean flag

The restored flag is redundant since it's always set to true when cancellableTask !=
null. Simplify the logic by checking cancellableTask != null directly in the finally
block to reduce unnecessary state tracking.

opensearch/src/main/java/org/opensearch/sql/opensearch/storage/scan/BackgroundSearchScanner.java [134-146]

 private OpenSearchResponse backgroundSearch(ProfileContext ctx, OpenSearchRequest request) {
-  boolean restored = false;
   if (cancellableTask != null) {
     OpenSearchQueryManager.setCancellableTask(cancellableTask);
-    restored = true;
   }
   try {
     return QueryProfiling.withCurrentContext(ctx, () -> client.search(request));
   } finally {
-    if (restored) {
+    if (cancellableTask != null) {
       OpenSearchQueryManager.clearCancellableTask();
     }
   }
 }
Suggestion importance[1-10]: 4

__

Why: The suggestion correctly identifies that the restored flag is redundant since it mirrors the cancellableTask != null check. However, this is a minor code style improvement that doesn't significantly impact functionality, correctness, or maintainability. The existing code is clear and the optimization is marginal.

Low

@Swiddis Swiddis changed the title fix: register background search tasks as children of parent fix: Register background search tasks as children of parent Oct 16, 2026
@codecov

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.86%. Comparing base (8e83f05) to head (c2db3b2).
⚠️ Report is 10 commits behind head on main.

❌ Your project check has failed because the head coverage (63.86%) is below the target coverage (99.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #5888      +/-   ##
============================================
+ Coverage     63.65%   63.86%   +0.21%     
- Complexity     9021     9097      +76     
============================================
  Files           959      962       +3     
  Lines         40823    41057     +234     
  Branches       4634     4671      +37     
============================================
+ Hits          25986    26223     +237     
+ Misses        13964    13956       -8     
- Partials        873      878       +5     
Flag Coverage Δ
sql-engine 63.86% <100.00%> (+0.21%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@RyanL1997 RyanL1997 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM - just left a question to clarify the scope of the original issue.

restored = true;
}
try {
return QueryProfiling.withCurrentContext(ctx, () -> client.search(request));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This covers an explicit _tasks/_cancel, but I think a query that ends any other way still leaves its in-flight search running:

  • On a timeout, OpenSearchQueryManager interrupts the worker thread.
  • On an early stop such as head, the enumerator closes and close() calls nextBatchFuture.cancel(true). CompletableFuture ignores mayInterruptIfRunning, so the prefetch keeps running.
  • In both cases the PPL task then finishes, and unregistering a parent doesn't cancel its children, so the search runs to completion with nobody waiting for it.

Just wanna leave the question here to see if that is in the scope for #5647?

@Swiddis Swiddis Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Timeouts are handled by a separate mechanism, this PR is orthogonal to that. I think in most timeout cases, the timeout propagates upwards from the underlying search task, not downward from the PPL coordinator thread.

On similar lines, nextBatchFuture's cancellation is orthogonal, and also seems pathological. You need to somehow have a join with individual pages taking multiple seconds for the problem to be measurable, and this just doesn't happen for most joins.

I think on both lines we can merge and file/fix separate bug reports iff they come up.

I also think it's ok if timeouts are looser since from the user perspective that behavior generally looks like "the broken query should be cleaned up eventually," whereas cancellation is more analogous to an emergency stop. Even in the worst case, the diff isn't more than ~15 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Zombie tasks on PPL cancellation

2 participants