Skip to content

Commit ee6f25b

Browse files
author
Andrew Haley
committed
8319120: Unbound ScopedValue.get() throws the wrong exception
Reviewed-by: alanb
1 parent e05cafd commit ee6f25b

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/java.base/share/classes/java/lang/ScopedValue.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,8 @@ private static Snapshot scopedValueBindings() {
787787
// Bindings can be in one of four states:
788788
//
789789
// 1: class Thread: this is a new Thread instance, and no
790-
// scoped values have ever been bound in this Thread.
790+
// scoped values have ever been bound in this Thread, and neither
791+
// have any scoped value bindings been inherited from a parent.
791792
// 2: EmptySnapshot.SINGLETON: This is effectively an empty binding.
792793
// 3: A Snapshot instance: this contains one or more scoped value
793794
// bindings.
@@ -798,18 +799,18 @@ private static Snapshot scopedValueBindings() {
798799
Object bindings = Thread.scopedValueBindings();
799800
if (bindings == NEW_THREAD_BINDINGS) {
800801
// This must be a new thread
801-
return Snapshot.EMPTY_SNAPSHOT;
802+
return Snapshot.EMPTY_SNAPSHOT;
802803
}
803804
if (bindings == null) {
804805
// Search the stack
805806
bindings = Thread.findScopedValueBindings();
806-
if (bindings == null) {
807-
// Nothing on the stack.
807+
if (bindings == NEW_THREAD_BINDINGS || bindings == null) {
808+
// We've walked the stack without finding anything.
808809
bindings = Snapshot.EMPTY_SNAPSHOT;
809810
}
811+
Thread.setScopedValueBindings(bindings);
810812
}
811813
assert (bindings != null);
812-
Thread.setScopedValueBindings(bindings);
813814
return (Snapshot) bindings;
814815
}
815816

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2023 Red Hat, Inc. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.util.NoSuchElementException;
25+
26+
/*
27+
* @test
28+
* @bug 8319120
29+
* @enablePreview
30+
* @run main/othervm -Xmx10m UnboundValueAfterOOME
31+
*/
32+
public class UnboundValueAfterOOME {
33+
34+
static final Thread doRun = new Thread() {
35+
public void run() {
36+
try {
37+
try {
38+
// Provoke the VM to throw an OutOfMemoryError
39+
java.util.Arrays.fill(new int[Integer.MAX_VALUE][], new int[Integer.MAX_VALUE]);
40+
} catch (OutOfMemoryError e) {
41+
// Try to get() an unbound ScopedValue
42+
ScopedValue.newInstance().get();
43+
}
44+
} catch (NoSuchElementException e) {
45+
System.out.println("OK");
46+
return;
47+
}
48+
throw new RuntimeException("Expected NoSuchElementException");
49+
}
50+
};
51+
52+
public static void main(String [] args) throws Exception {
53+
doRun.run(); // Run on this Thread
54+
var job = new Thread(doRun);
55+
job.start(); // Run on a new Thread
56+
job.join();
57+
doRun.start(); // Run on the Thread doRun
58+
doRun.join();
59+
}
60+
}

0 commit comments

Comments
 (0)