Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified URLResource cleaner #10061

Merged
merged 3 commits into from
Jul 5, 2023

Conversation

gregw
Copy link
Contributor

@gregw gregw commented Jul 4, 2023

The reference to the InputStream does not need to be soft/weak as a hard reference to it does not prevent the URLResource itself from being collected. Also by making a runnable record, there is one less allocation per input stream creation.

The reference to the InputStream does not need to be soft/weak as a hard reference to it does not prevent the URLResource itself from being collected.  Also by making a runnable record, there is one less allocation per input stream creation.
@gregw gregw requested review from joakime and lorban July 4, 2023 11:27
if (ON_SWEEP_LISTENER != null)
ON_SWEEP_LISTENER.accept(ref);
});
CLEANER.register(this, new InputStreamCleaner(in));
Copy link
Contributor

Choose a reason for hiding this comment

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

Rereading this code, I noticed the input stream gets closed if the URLResource gets garbage collected.

This means we get this:

Resource webResource = resource.resolve("/web.xml");
assertTrue(webResource.exists());
InputStream inputStream = webResource.newInputStream();
webResource = null;

await().atMost(5, TimeUnit.SECONDS).until(() ->
{
    System.gc();
    return cleanedRefCount.get() > 0;
});

assertThat(IO.toString(inputStream), notNullValue()); //  this throws IOException: Stream closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm perhaps we need to weak reference the input stream itself?

But it will generally be fine as I don't the reference to the input stream outlives the resource reference often.

Either way, this is better than what is committed.

lorban
lorban previously approved these changes Jul 4, 2023
@gregw
Copy link
Contributor Author

gregw commented Jul 4, 2023

@lorban thanks for the green tick... But I think I have an idea to simply solve that problem... I'll try tonight....

Use AtomicReference for the inputstream, so that if it is taken, then it is nulled and will not be closed.
@gregw
Copy link
Contributor Author

gregw commented Jul 4, 2023

@lorban what about now?

@gregw gregw requested a review from lorban July 4, 2023 16:58
@gregw
Copy link
Contributor Author

gregw commented Jul 4, 2023

@lorban I think the test is a bit flakey. sometimes it does not complete when I run it from the IDE

@gregw
Copy link
Contributor Author

gregw commented Jul 5, 2023

@lorban I fixed the tests and now test for the inputstream not being closed if it is taken

Copy link
Contributor

@lorban lorban left a comment

Choose a reason for hiding this comment

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

LGTM

@gregw gregw merged commit 32dc0c8 into jetty-12.0.x Jul 5, 2023
2 checks passed
@gregw gregw deleted the fix/jetty-12-simplify-urlResource-cleaner branch July 5, 2023 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants