You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following code snippets are in 3.9.4.final.
In org.jboss.netty.channel.socket.nio.AbstractNioBossPool, method
protected void init() {
if (initialized) {
throw new IllegalStateException("initialized already");
}
initialized = true;
...
}
where the definition of variable "initialized" is
private volatile boolean initialized;
Is there concurrency hazard that may cause the init() method to be called more than once?
If 2 threads were call the init() method simultaneously. Thread1 checks that if(initialized) is false and hang, Thread2 now checks if(initialized) is false and do the initialization. Then the Thread1 is back to run, and do the initialization again.
I think it's much better to make the variable "initialized" AtomicBoolean, because you can use atomic CAS to do the initialization exactly once:
private final AtomicBoolean initialized = new AtomicBoolean(false);
protected void init() {
if (!initialized.compareAndSet(false, true)) {
throw new IllegalStateException("initialized already");
}
....
}
The text was updated successfully, but these errors were encountered:
…tractNioBoss(Worker)Pool.
Motivation:
During the reading of the source codes of Netty 3.9.5.Final, I thought there might have a lurking concurrency bug in the AbstractNioBossPool#init method, of which a single volatile variable cannot correctly control the initialization sequence happened exactly only once under concurrency environment. Also I found there's already a much more elegant and concurrency safe way to do the work like this, I decided to make this PR. (Please refer to the discussion in #3249.)
Modifications:
Change the type of the variable that control the initialization from "volatile boolean" to "final AtomicBoolean".
Result:
The potential concurrency hazard of the initialization in AbstractNioBoss(Worker)Pool will be eliminated.
The following code snippets are in 3.9.4.final.
In org.jboss.netty.channel.socket.nio.AbstractNioBossPool, method
where the definition of variable "initialized" is
Is there concurrency hazard that may cause the init() method to be called more than once?
If 2 threads were call the init() method simultaneously. Thread1 checks that if(initialized) is false and hang, Thread2 now checks if(initialized) is false and do the initialization. Then the Thread1 is back to run, and do the initialization again.
I think it's much better to make the variable "initialized" AtomicBoolean, because you can use atomic CAS to do the initialization exactly once:
The text was updated successfully, but these errors were encountered: