Skip to content
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

AsyncContextImpl$future字段没有并发控制 #6572

Open
Ech0Fan opened this issue Aug 10, 2020 · 1 comment
Open

AsyncContextImpl$future字段没有并发控制 #6572

Ech0Fan opened this issue Aug 10, 2020 · 1 comment

Comments

@Ech0Fan
Copy link

Ech0Fan commented Aug 10, 2020

  • Dubbo version: latest

https://github.com/apache/dubbo/blob/dubbo-2.7.8/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncContextImpl.java

private final AtomicBoolean started = new AtomicBoolean(false);
private CompletableFuture<Object> future;

public void start() {
    if (this.started.compareAndSet(false, true)) {
        this.future = new CompletableFuture<>();
    }
 }

public void write(Object value) {
    if (isAsyncStarted() && stop()) {
        if (value instanceof Throwable) {
            Throwable bizExe = (Throwable) value;
            future.completeExceptionally(bizExe);
        } else {
            future.complete(value);
        }
    } else {
        // ignored
    }
}

isAsyncStarted()返回true时确定能保证future字段的可见性么?

@ljluestc
Copy link

为了确保 future 字段在被多个线程访问时的可见性和一致性,您可以将其声明为 volatile,或者使用适当的同步机制(如同步块或 Lock 对象)在 future 字段的写入和读取操作之间建立发生之前关系 。

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

No branches or pull requests

2 participants