Skip to content

Commit a0f7604

Browse files
committed
8375125: assert(false) failed: "Attempting to acquire lock NativeHeapTrimmer_lock/nosafepoint out of order with lock ConcurrentHashTableResize_lock/nosafepoint-2 -- possible deadlock" when using native heap trimmer
Backport-of: a67979c4e6dcea70e63cc79a105be12a9306c660
1 parent fcc82d0 commit a0f7604

3 files changed

Lines changed: 117 additions & 4 deletions

File tree

src/hotspot/share/classfile/stringTable.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -615,14 +615,17 @@ struct StringTableDeleteCheck : StackObj {
615615
};
616616

617617
void StringTable::clean_dead_entries(JavaThread* jt) {
618+
// BulkDeleteTask::prepare() may take ConcurrentHashTableResize_lock (nosafepoint-2).
619+
// When NativeHeapTrimmer is enabled, SuspendMark may take NativeHeapTrimmer::_lock (nosafepoint).
620+
// Take SuspendMark first to keep lock order and avoid deadlock.
621+
NativeHeapTrimmer::SuspendMark sm("stringtable");
618622
StringTableHash::BulkDeleteTask bdt(_local_table);
619623
if (!bdt.prepare(jt)) {
620624
return;
621625
}
622626

623627
StringTableDeleteCheck stdc;
624628
StringTableDoDelete stdd;
625-
NativeHeapTrimmer::SuspendMark sm("stringtable");
626629
{
627630
TraceTime timer("Clean", TRACETIME_LOG(Debug, stringtable, perf));
628631
while(bdt.do_task(jt, stdc, stdd)) {

src/hotspot/share/classfile/symbolTable.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -765,14 +765,17 @@ struct SymbolTableDeleteCheck : StackObj {
765765
};
766766

767767
void SymbolTable::clean_dead_entries(JavaThread* jt) {
768+
// BulkDeleteTask::prepare() may take ConcurrentHashTableResize_lock (nosafepoint-2).
769+
// When NativeHeapTrimmer is enabled, SuspendMark may take NativeHeapTrimmer::_lock (nosafepoint).
770+
// Take SuspendMark first to keep lock order and avoid deadlock.
771+
NativeHeapTrimmer::SuspendMark sm("symboltable");
768772
SymbolTableHash::BulkDeleteTask bdt(_local_table);
769773
if (!bdt.prepare(jt)) {
770774
return;
771775
}
772776

773777
SymbolTableDeleteCheck stdc;
774778
SymbolTableDoDelete stdd;
775-
NativeHeapTrimmer::SuspendMark sm("symboltable");
776779
{
777780
TraceTime timer("Clean", TRACETIME_LOG(Debug, symboltable, perf));
778781
while (bdt.do_task(jt, stdc, stdd)) {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2026, Oracle and/or its affiliates. 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+
/**
25+
* @test
26+
* @bug 8375125
27+
* @summary Trigger StringTable::clean_dead_entries or SymbolTable::clean_dead_entries
28+
* with -XX:TrimNativeHeapInterval enabled,should not violate lock ordering.
29+
* @requires vm.debug
30+
* @requires vm.gc != "Epsilon"
31+
* @library /test/lib
32+
* @modules java.compiler
33+
* @run main/othervm -Xms128m -Xmx128m
34+
* -XX:TrimNativeHeapInterval=300000
35+
* TestTrimNativeHeapIntervalTablesCleanup string
36+
* @run main/othervm -Xms128m -Xmx128m
37+
* -XX:TrimNativeHeapInterval=300000
38+
* TestTrimNativeHeapIntervalTablesCleanup symbol
39+
*/
40+
41+
import java.util.LinkedList;
42+
import jdk.test.lib.compiler.InMemoryJavaCompiler;
43+
44+
public class TestTrimNativeHeapIntervalTablesCleanup {
45+
46+
public static void main(String[] args) throws Exception{
47+
if (args.length != 1) {
48+
throw new IllegalArgumentException("Expected 1 argument: string|symbol");
49+
}
50+
switch (args[0]) {
51+
case "string":
52+
testStringTableCleanup();
53+
break;
54+
case "symbol":
55+
testSymbolTableCleanup();
56+
break;
57+
default:
58+
throw new IllegalArgumentException("Unknown mode: " + args[0]);
59+
}
60+
System.out.println("passed: " + args[0]);
61+
}
62+
63+
static void testStringTableCleanup() throws Exception{
64+
final int rounds = 30;
65+
final int maxSize = 200_000;
66+
final int pruneEvery = 50_000;
67+
final int pruneCount = 25_000;
68+
long stringNum = 0;
69+
70+
for (int round = 0; round < rounds; round++) {
71+
LinkedList<String> list = new LinkedList<>();
72+
for (int i = 0; i < maxSize; i++, stringNum++) {
73+
if (i != 0 && (i % pruneEvery) == 0) {
74+
int toRemove = Math.min(pruneCount, list.size());
75+
list.subList(0, toRemove).clear();
76+
}
77+
list.push(Long.toString(stringNum).intern());
78+
}
79+
System.gc();
80+
Thread.sleep(1000);
81+
}
82+
}
83+
84+
static void testSymbolTableCleanup() throws Exception {
85+
final int rounds = 10;
86+
final int classesPerRound = 100;
87+
88+
for (int r = 0; r < rounds; r++) {
89+
for (int i = 0; i < classesPerRound; i++) {
90+
String cn = "C" + r + "_" + i;
91+
byte[] bytes = InMemoryJavaCompiler.compile(
92+
cn,
93+
"public class " + cn + " { int m" + i + "() { return " + i + "; } }"
94+
);
95+
new ClassLoader(null) {
96+
@Override
97+
protected Class<?> findClass(String name) throws ClassNotFoundException {
98+
if (!name.equals(cn)) throw new ClassNotFoundException(name);
99+
return defineClass(name, bytes, 0, bytes.length);
100+
}
101+
}.loadClass(cn).getDeclaredConstructor().newInstance();
102+
}
103+
System.gc();
104+
Thread.sleep(1000);
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)