diff options
author | Torbjörn SVENSSON | 2021-11-15 10:52:11 +0000 |
---|---|---|
committer | Sarika Sinha | 2021-11-17 18:29:19 +0000 |
commit | f3956032ba74e6bcb3729a5ba805497fe59c76c3 (patch) | |
tree | b51b4094f5c79b8f8b0e64f620e44a2c99d72961 | |
parent | a7533bd84d2276ba3180174559cb3a25d3430db8 (diff) | |
download | eclipse.platform.debug-I20211120-1800.tar.gz eclipse.platform.debug-I20211120-1800.tar.xz eclipse.platform.debug-I20211120-1800.zip |
Bug 577265: Handle the unlikely race in close() methodS4_22_0_RC2S4_22_0_RC1R4_22I20211124-1800I20211124-0600I20211123-1800I20211123-0750I20211123-0600I20211122-1800I20211122-0820I20211121-1800I20211121-0600I20211120-1800I20211120-0600I20211119-1800I20211117-1830
There was a race between checking the fThread for null and seting it
to null. This could lead to that two parallel calls to close() could
be made and the 2nd call would get an NPE.
Change-Id: Ia107f6c84c4092a09027c691646e343d400ca6d4
Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@st.com>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.debug/+/187734
Reviewed-by: Jonah Graham <jonah@kichwacoders.com>
Reviewed-by: Sarika Sinha <sarika.sinha@in.ibm.com>
Tested-by: Sarika Sinha <sarika.sinha@in.ibm.com>
-rw-r--r-- | org.eclipse.debug.core/core/org/eclipse/debug/internal/core/InputStreamMonitor.java | 19 | ||||
-rw-r--r-- | org.eclipse.debug.core/core/org/eclipse/debug/internal/core/OutputStreamMonitor.java | 28 |
2 files changed, 30 insertions, 17 deletions
diff --git a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/InputStreamMonitor.java b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/InputStreamMonitor.java index 7ed21a6ea..d0525f642 100644 --- a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/InputStreamMonitor.java +++ b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/InputStreamMonitor.java @@ -131,10 +131,12 @@ public class InputStreamMonitor { * Starts a thread which writes the stream. */ public void startMonitoring() { - if (fThread == null) { - fThread = new Thread((Runnable) this::write, DebugCoreMessages.InputStreamMonitor_label); - fThread.setDaemon(true); - fThread.start(); + synchronized (this) { + if (fThread == null) { + fThread = new Thread((Runnable) this::write, DebugCoreMessages.InputStreamMonitor_label); + fThread.setDaemon(true); + fThread.start(); + } } } @@ -143,9 +145,12 @@ public class InputStreamMonitor { * monitor and the underlying stream. */ public void close() { - if (fThread != null) { - Thread thread= fThread; - fThread= null; + Thread thread = null; + synchronized (this) { + thread = fThread; + fThread = null; + } + if (thread != null) { thread.interrupt(); } } diff --git a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/OutputStreamMonitor.java b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/OutputStreamMonitor.java index bdc2cd539..3a6f1a41c 100644 --- a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/OutputStreamMonitor.java +++ b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/OutputStreamMonitor.java @@ -134,15 +134,21 @@ public class OutputStreamMonitor implements IBinaryStreamMonitor { * underlying stream by waiting for the thread to terminate. */ protected void close() { - if (fThread != null) { - Thread thread = fThread; + Thread thread = null; + + synchronized (this) { + thread = fThread; fThread = null; + } + + if (thread != null) { try { thread.join(); } catch (InterruptedException ie) { } - fListeners = new ListenerList<>(); - fBinaryListeners = new ListenerList<>(); + + fListeners.clear(); + fBinaryListeners.clear(); } } @@ -304,12 +310,14 @@ public class OutputStreamMonitor implements IBinaryStreamMonitor { * Starts a thread which reads from the stream */ protected void startMonitoring() { - if (fThread == null) { - fDone.set(false); - fThread = new Thread((Runnable) this::read, DebugCoreMessages.OutputStreamMonitor_label); - fThread.setDaemon(true); - fThread.setPriority(Thread.MIN_PRIORITY); - fThread.start(); + synchronized (this) { + if (fThread == null) { + fDone.set(false); + fThread = new Thread((Runnable) this::read, DebugCoreMessages.OutputStreamMonitor_label); + fThread.setDaemon(true); + fThread.setPriority(Thread.MIN_PRIORITY); + fThread.start(); + } } } |