Skip to content

Commit c4ec983

Browse files
roberttoyonagatstuefe
authored andcommitted
8370715: JFR: Races are possible when dumping recordings
Reviewed-by: egahlin, stuefe
1 parent bcbdf90 commit c4ec983

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/jdk.jfr/share/classes/jdk/jfr/internal/PlatformRecording.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.IOException;
3535
import java.io.InputStream;
3636
import java.nio.channels.FileChannel;
37+
import java.nio.channels.FileLock;
3738
import java.nio.file.NoSuchFileException;
3839
import java.nio.file.Path;
3940
import java.nio.file.StandardOpenOption;
@@ -744,7 +745,14 @@ private void transferChunksWithRetry(WriteablePath path) throws IOException {
744745
}
745746

746747
private void transferChunks(WriteablePath path) throws IOException {
747-
try (ChunksChannel cc = new ChunksChannel(chunks); FileChannel fc = FileChannel.open(path.getReal(), StandardOpenOption.WRITE, StandardOpenOption.APPEND)) {
748+
// Before writing, wipe the file if it already exists.
749+
try (ChunksChannel cc = new ChunksChannel(chunks); FileChannel fc = FileChannel.open(path.getReal(), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
750+
// Mitigate races against other processes
751+
FileLock l = fc.tryLock();
752+
if (l == null) {
753+
Logger.log(LogTag.JFR, LogLevel.INFO, "Dump operation skipped for recording \"" + name + "\" (" + id + "). File " + path.getRealPathText() + " is locked by other dump operation or activity.");
754+
return;
755+
}
748756
long bytes = cc.transferTo(fc);
749757
Logger.log(LogTag.JFR, LogLevel.INFO, "Transferred " + bytes + " bytes from the disk repository");
750758
// No need to force if no data was transferred, which avoids IOException when device is /dev/null
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2025, 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+
package jdk.jfr.api.recording.dump;
25+
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
29+
30+
import jdk.jfr.Recording;
31+
import jdk.jfr.consumer.RecordedEvent;
32+
import jdk.jfr.consumer.RecordingFile;
33+
import jdk.test.lib.Asserts;
34+
import jdk.test.lib.jfr.SimpleEventHelper;
35+
import jdk.test.lib.jfr.Events;
36+
37+
/**
38+
* @test
39+
* @summary Test that multiple dumps to the same file by ongoing recordings do not mangle data.
40+
* @requires vm.flagless
41+
* @requires vm.hasJFR
42+
* @library /test/lib
43+
* @run main/othervm jdk.jfr.api.recording.dump.TestDumpOverwrite
44+
*/
45+
public class TestDumpOverwrite {
46+
private static Path DUMP_PATH = Paths.get(".", "rec_TestDumpOverwrite.jfr");
47+
public static void main(String[] args) throws Exception {
48+
Recording recording1 = new Recording();
49+
Recording recording2 = new Recording();
50+
SimpleEventHelper.enable(recording1, true);
51+
SimpleEventHelper.enable(recording2, true);
52+
recording1.setDestination(DUMP_PATH);
53+
recording2.setDestination(DUMP_PATH);
54+
55+
int actualId = 0;
56+
recording1.start();
57+
SimpleEventHelper.createEvent(actualId++);
58+
recording2.start();
59+
SimpleEventHelper.createEvent(actualId++);
60+
// This is results in the initial write to the dump destination
61+
recording2.stop();
62+
SimpleEventHelper.createEvent(actualId++);
63+
recording2.close();
64+
SimpleEventHelper.createEvent(actualId++);
65+
// This should first wipe the data previously written by recording2.
66+
recording1.stop();
67+
recording1.close();
68+
69+
Asserts.assertTrue(Files.exists(DUMP_PATH), "Recording file does not exist: " + DUMP_PATH);
70+
71+
// Verify events are read in order without duplicates (otherwise chunks may be out of order).
72+
// If the dump file is not being overwritten correctly, we will see event ids: 1, 0, 1, 2, 3.
73+
int expectedId = 0;
74+
for (RecordedEvent event : RecordingFile.readAllEvents(DUMP_PATH)) {
75+
Events.assertField(event, "id").equal(expectedId++);
76+
}
77+
Asserts.assertTrue(expectedId == actualId, "incorrect number of events found");
78+
}
79+
}

0 commit comments

Comments
 (0)