-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add stack-safe async loop support with trampoline pattern #1905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
2c80db1
cfef056
9d38e5f
62f4bb3
7d9bf06
76f5f13
dbbe307
4c7f0de
ecb7ad8
016aad9
5114a5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.mongodb.internal.async; | ||
|
|
||
| import com.mongodb.annotations.NotThreadSafe; | ||
| import com.mongodb.lang.Nullable; | ||
|
|
||
| /** | ||
| * A trampoline that converts recursive callback invocations into an iterative loop, | ||
| * preventing stack overflow in async loops. | ||
| * | ||
| * <p>When async loop iterations complete synchronously on the same thread, callback | ||
| * recursion occurs: each iteration's {@code callback.onResult()} immediately triggers | ||
| * the next iteration, causing unbounded stack growth. For example, a 1000-iteration | ||
| * loop would create > 1000 stack frames and cause {@code StackOverflowError}.</p> | ||
| * | ||
| * <p>The trampoline intercepts this recursion: instead of executing the next iteration | ||
| * immediately (which would deepen the stack), it enqueues the continuation and returns, allowing | ||
| * the stack to unwind. A flat loop at the top then processes enqueued continuation iteratively, | ||
| * maintaining constant stack depth regardless of iteration count.</p> | ||
| * | ||
| * <p>Since async chains are sequential, at most one task is pending at any time. | ||
| * The trampoline uses a single slot rather than a queue.</p> | ||
| * | ||
| * The first call on a thread becomes the "trampoline owner" and runs the drain loop. | ||
| * Subsequent (re-entrant) calls on the same thread enqueue their continuation and return immediately.</p> | ||
| * | ||
| * <p>This class is not part of the public API and may be removed or changed at any time</p> | ||
| */ | ||
| @NotThreadSafe | ||
| public final class AsyncTrampoline { | ||
|
|
||
| private static final ThreadLocal<ContinuationHolder> TRAMPOLINE = new ThreadLocal<>(); | ||
|
|
||
| private AsyncTrampoline() {} | ||
|
|
||
| /** | ||
| * Execute continuation through the trampoline. If no trampoline is active, become the owner | ||
| * and drain all enqueued continuations. If a trampoline is already active, enqueue and return. | ||
| */ | ||
| public static void run(final Runnable continuation) { | ||
| ContinuationHolder continuationHolder = TRAMPOLINE.get(); | ||
| if (continuationHolder != null) { | ||
| continuationHolder.enqueue(continuation); | ||
| } else { | ||
| continuationHolder = new ContinuationHolder(); | ||
| TRAMPOLINE.set(continuationHolder); | ||
| try { | ||
| continuation.run(); | ||
| while (continuationHolder.continuation != null) { | ||
| Runnable continuationToRun = continuationHolder.continuation; | ||
| continuationHolder.continuation = null; | ||
| continuationToRun.run(); | ||
| } | ||
| } finally { | ||
| TRAMPOLINE.remove(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A single-slot container for continuation. | ||
| * At most one continuation is pending at any time in a sequential async chain. | ||
| */ | ||
| @NotThreadSafe | ||
| private static final class ContinuationHolder { | ||
| @Nullable | ||
| private Runnable continuation; | ||
|
|
||
| void enqueue(final Runnable continuation) { | ||
| if (this.continuation != null) { | ||
| throw new AssertionError("Trampoline slot already occupied"); | ||
| } | ||
| this.continuation = continuation; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ | ||||||||||||||||
| package com.mongodb.internal.async.function; | |||||||||||||||||
|
|
|||||||||||||||||
| import com.mongodb.annotations.NotThreadSafe; | |||||||||||||||||
| import com.mongodb.internal.async.AsyncTrampoline; | |||||||||||||||||
| import com.mongodb.internal.async.SingleResultCallback; | |||||||||||||||||
| import com.mongodb.lang.Nullable; | |||||||||||||||||
|
|
|||||||||||||||||
|
|
@@ -62,9 +63,11 @@ public void run(final SingleResultCallback<Void> callback) { | ||||||||||||||||
| @NotThreadSafe | |||||||||||||||||
| private class LoopingCallback implements SingleResultCallback<Void> { | |||||||||||||||||
| private final SingleResultCallback<Void> wrapped; | |||||||||||||||||
| private final Runnable nextIteration; | |||||||||||||||||
|
|
|||||||||||||||||
| LoopingCallback(final SingleResultCallback<Void> callback) { | |||||||||||||||||
| wrapped = callback; | |||||||||||||||||
| nextIteration = () -> body.run(this); | |||||||||||||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The nextIteration is reused to avoid creation of extra objects via
The
For Lambda case:
vbabanin marked this conversation as resolved.
Outdated
|
|||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| @Override | |||||||||||||||||
|
|
@@ -80,7 +83,7 @@ public void onResult(@Nullable final Void result, @Nullable final Throwable t) { | ||||||||||||||||
| return; | |||||||||||||||||
| } | |||||||||||||||||
| if (continueLooping) { | |||||||||||||||||
| body.run(this); | |||||||||||||||||
| AsyncTrampoline.run(nextIteration); | |||||||||||||||||
| } else { | |||||||||||||||||
| wrapped.onResult(result, null); | |||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.