Skip to content
This repository was archived by the owner on Sep 8, 2022. It is now read-only.

Commit 607df99

Browse files
committed
Use a store fence to try to avoid race condition with sys.props access
1 parent fbeb205 commit 607df99

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/main/scala/scala/tools/partest/nest/StreamCapture.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ object StreamCapture {
4343
val modified = new java.util.Properties()
4444
modified.putAll(saved)
4545
extra.foreach { case (k, v) => modified.setProperty(k, v) }
46+
// Trying to avoid other threads seeing the new properties object prior to the new entries
47+
// https://github.com/scala/scala/pull/6391#issuecomment-371346171
48+
UnsafeAccess.U.storeFence()
4649
System.setProperties(modified)
4750
try {
4851
action
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package scala.tools.partest.nest;
2+
3+
import java.lang.reflect.Field;
4+
5+
@SuppressWarnings("unsafe")
6+
public class UnsafeAccess {
7+
public final static sun.misc.Unsafe U;
8+
9+
static {
10+
U = lookupUnsafe();
11+
}
12+
13+
private static sun.misc.Unsafe lookupUnsafe() {
14+
try {
15+
sun.misc.Unsafe found = null;
16+
for (Field field : sun.misc.Unsafe.class.getDeclaredFields()) {
17+
if (field.getType() == sun.misc.Unsafe.class) {
18+
field.setAccessible(true);
19+
found = (sun.misc.Unsafe) field.get(null);
20+
break;
21+
}
22+
}
23+
if (found == null) throw new IllegalStateException("Can't find instance of sun.misc.Unsafe");
24+
else return found;
25+
} catch (Throwable t) {
26+
throw new ExceptionInInitializerError(t);
27+
}
28+
}
29+
}
30+

0 commit comments

Comments
 (0)