Skip to content

Conversation

chiroito
Copy link
Member

@chiroito chiroito commented Aug 9, 2025

Fix an issue where RecordingStream and RemoteRecordingStream do not stop when their underlying the local/remote recording stop.

Introduce an internal EventSource abstraction to unify control across local (PlatformRecording) and remote (FlightRecorderMXBean) recordings, ensuring consistent propagation of stop/close and stop time to the consumer streams.

Tests updated to validate lifecycle consistency:
Verifies that stopping/closing the underlying recording (local and remote) properly stops/closes the corresponding stream.

Test: jdk/jdk/jfr


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8365066: RecordingStream and RemoteRecordingStream do not terminate when the associated Recording is stopped or closed externally (Bug - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26710/head:pull/26710
$ git checkout pull/26710

Update a local copy of the PR:
$ git checkout pull/26710
$ git pull https://git.openjdk.org/jdk.git pull/26710/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26710

View PR using the GUI difftool:
$ git pr show -t 26710

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26710.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 9, 2025

👋 Welcome back cito! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 9, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Aug 9, 2025

@chiroito The following label will be automatically applied to this pull request:

  • hotspot-jfr

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@egahlin
Copy link
Member

egahlin commented Aug 9, 2025

Did you look into adding a FlightRecorderListener and implementing recordingStateChanged to check if the RecordingState is CLOSED and then call close() on the stream? Or perhaps it would be better to set a flag and let the thread running the stream close itself after the next flush is complete.

For a RemoteRecordingStream there is a JMX notification that can be listened on.

private static class SendEventListener implements FlightRecorderListener {
@Override
public void recordingStateChanged(Recording recording) {
if(recording.getState() == RecordingState.RUNNING){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(recording.getState() == RecordingState.RUNNING){
if (recording.getState() == RecordingState.RUNNING){

private static class SendEventListener implements FlightRecorderListener {
@Override
public void recordingStateChanged(Recording recording) {
if(recording.getState() == RecordingState.RUNNING){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(recording.getState() == RecordingState.RUNNING){
if (recording.getState() == RecordingState.RUNNING){

@@ -469,7 +467,9 @@ public void close() {
ManagementSupport.setOnChunkCompleteHandler(stream, null);
stream.close();
try {
mbean.closeRecording(recordingId);
if(eventSource.getState() != RecordingState.CLOSED) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(eventSource.getState() != RecordingState.CLOSED) {
if (eventSource.getState() != RecordingState.CLOSED) {

}

@Override
public RecordingState getState() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public RecordingState getState() {
public RecordingState getState() {

@@ -169,6 +167,19 @@ protected void processRecursionSafe() throws IOException {
"ns (epoch), parser at " + lastFlush + "ns (epoch).");
return;
}

if(!barrier.used()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(!barrier.used()) {
if (!barrier.used()) {

if (isRecordingStream()) {
if (recording.getState() == RecordingState.STOPPED && !barrier.used()) {
logStreamEnd("recording stopped externally.");
if(!barrier.used()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(!barrier.used()) {
if (!barrier.used()) {

if (state == RecordingState.CLOSED){
logStreamEnd("Event source is closed externally");
return;
}else if(state == RecordingState.STOPPED) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}else if(state == RecordingState.STOPPED) {
} else if (state == RecordingState.STOPPED) {

private static class SendEventListener implements FlightRecorderListener{
@Override
public void recordingStateChanged(Recording recording) {
if(recording.getState() == RecordingState.RUNNING){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(recording.getState() == RecordingState.RUNNING){
if (recording.getState() == RecordingState.RUNNING){

private static class SendEventListener implements FlightRecorderListener {
@Override
public void recordingStateChanged(Recording recording) {
if(recording.getState() == RecordingState.RUNNING){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(recording.getState() == RecordingState.RUNNING){
if (recording.getState() == RecordingState.RUNNING){

@@ -40,21 +41,44 @@
*/
public class TestStoppedRecording {

private static class SendEventListener implements FlightRecorderListener{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static class SendEventListener implements FlightRecorderListener{
private static class SendEventListener implements FlightRecorderListener {

@Override
public long getStopTime() {
Optional<RecordingInfo> recordingInfo = mbean.getRecordings().stream().filter(r -> r.getId() == recordingId).findFirst();
if(recordingInfo.isEmpty()){
Copy link
Member

@turbanoff turbanoff Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(recordingInfo.isEmpty()){
if (recordingInfo.isEmpty()){

}

@Override
public RecordingState getState() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public RecordingState getState() {
public RecordingState getState() {

@Override
public RecordingState getState() {
Optional<RecordingInfo> recordingInfo = mbean.getRecordings().stream().filter(r -> r.getId() == recordingId).findFirst();
if(recordingInfo.isEmpty()){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(recordingInfo.isEmpty()){
if (recordingInfo.isEmpty()){

public RecordingState getState() {
Optional<RecordingInfo> recordingInfo = mbean.getRecordings().stream().filter(r -> r.getId() == recordingId).findFirst();
if(recordingInfo.isEmpty()){
return RecordingState.CLOSED;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return RecordingState.CLOSED;
return RecordingState.CLOSED;

@chiroito
Copy link
Member Author

@egahlin That sounds like a good idea. I’ll give it a try.

…hen the associated Recording is stopped or closed externally
@openjdk
Copy link

openjdk bot commented Aug 21, 2025

@chiroito Please do not rebase or force-push to an active PR as it invalidates existing review comments. Note for future reference, the bots always squash all changes into a single commit automatically as part of the integration. See OpenJDK Developers’ Guide for more information.

@openjdk openjdk bot added the rfr Pull request is ready for review label Aug 21, 2025
@mlbridge
Copy link

mlbridge bot commented Aug 21, 2025

Webrevs

@chiroito
Copy link
Member Author

@egahlin
Could you please review this please?
I added a listener-based mechanism that propagates external stop/close to the stream.

@egahlin
Copy link
Member

egahlin commented Aug 21, 2025

@egahlin Could you please review this please? I added a listener-based mechanism that propagates external stop/close to the stream.

This will take me some time to go through. I'm a little bit worried about the locking, but I need to investigate, before I can comment further.

@chiroito
Copy link
Member Author

@egahlin

I apologize.
I forgot to fix some code I had been using to test the locking behavior and mistakenly committed it.
I’ll correct this and push the changes soon.

I do have one question: is there a general preference between using synchronized and ReentrantLock?
Personally, I lean toward ReentrantLock, since it can be safely used with virtual threads and may be easier to backport if needed.

@egahlin
Copy link
Member

egahlin commented Aug 24, 2025

@egahlin

I apologize. I forgot to fix some code I had been using to test the locking behavior and mistakenly committed it. I’ll correct this and push the changes soon.

I do have one question: is there a general preference between using synchronized and ReentrantLock? Personally, I lean toward ReentrantLock, since it can be safely used with virtual threads and may be easier to backport if needed.

The preference is to not use locks at all when callbacks (listeners) are being invoked as it could potentially lead to dead locks. Best would be if a flag can be set so the stream stops or closes itself at the next flush, but again, I haven't had time to look at it, so it might not be feasible. Best behavior would be if an external stop or close acted as an internal one. The code with the stream barrier is complex, but it was needed to ensure all subscribed events emitted before a stop gets an onEvent(...) callback.

@egahlin
Copy link
Member

egahlin commented Aug 31, 2025

I've looked into the issue, and I'm not sure a RemoteRecordingStream should be closed if the underlying recording is.

  • One use case for the RemoteRecordingStream is to replicate the repository on another host. When the monitored application exits (and closes the recording), the data should be available on another machine. If we close the stream, data will be lost (prematurely). I need to think about this some more.

  • I don't think we need to do anything when the underlying recording is stopped for RemoteRecordingStream, since a stopped stream should wait until all events in the recording have been consumed. The DownloadThread will end when there is no more data to be read from FlightRecorderMXBean::readStream.

  • For a RecordingStream, I think Recording::stop/close can just delegate to RecordingStream::stop/close and we can get the exact same behavior as if the stream was stopped/closed.

I have implemented the above changes. You can look at it here #27025

I noticed that the underlying Recording object may be exposed before the stream has been initialized, so I added synchronization. Now, any other thread trying to get a recording will have to wait for the RecordingStream initialization to complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-jfr [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants