Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Conversation

@jordmoz
Copy link
Contributor

@jordmoz jordmoz commented Jun 8, 2015

https://java.net/jira/browse/JERSEY-2291

Hopefully addresses the above issue.

@jerseyrobot
Copy link
Contributor

Can one of the admins verify this patch?

@AdamLindenthal
Copy link
Member

Hi Jord,
many thanks for contributing.
Before we can review your pull request, we need you to sign Oracle Contributor Agreement.

More information about contributing to Jersey: https://jersey.java.net/scm.html#/Submitting_Patches_and_Contribute_Code

Thanks,
Adam

@AdamLindenthal
Copy link
Member

Jenkins, please test this patch.

@jordmoz
Copy link
Contributor Author

jordmoz commented Jun 10, 2015

Thanks, I just submitted my OCA today.

@mpotociar mpotociar added the OCA label Jun 15, 2015
@mpotociar
Copy link
Collaborator

Hi Jord,

In order to accept your patch, it needs to come up with at least one test that verifies the enhancement works as expected (your case) or that contains a reproducer for the fixed bug (in case of a bug fix patch).

Cheers,
Marek

@jordmoz jordmoz force-pushed the master branch 2 times, most recently from b2cb5e5 to 317cee1 Compare June 15, 2015 18:30
@jordmoz
Copy link
Contributor Author

jordmoz commented Jun 15, 2015

I have added a test case which verifies the added functionality. Please take another look.

@jordmoz
Copy link
Contributor Author

jordmoz commented Jun 22, 2015

I think the test failure from before may have been spurious. I just synced my local fork with master and re-ran the failed test and it passed. Please take another look.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@mgajdos
Copy link
Contributor

mgajdos commented Jun 24, 2015

Overall looks good. Please, fix 2 minor issues and we'll merge this pull request.

@jordmoz
Copy link
Contributor Author

jordmoz commented Jun 24, 2015

I have addressed all review comments. Please take another look.

@mgajdos
Copy link
Contributor

mgajdos commented Jun 24, 2015

Jenkins, test this patch.

@mgajdos
Copy link
Contributor

mgajdos commented Jun 24, 2015

There is one failing test - seems it's caused by your change (verified on my machine). I'll take a look at it tomorrow.

@jordmoz
Copy link
Contributor Author

jordmoz commented Jun 24, 2015

I am unable to fail that test. I've synced up to jersey head and am doing
'mvn test' in the core-client directory. What am I doing wrong?

On Wed, Jun 24, 2015 at 11:41 AM, Michal Gajdos notifications@github.com
wrote:

There is one failing test - seems it's caused by your change (verified on
my machine). I'll take a look at it tomorrow.


Reply to this email directly or view it on GitHub
https://github.com/jersey/jersey/pull/173#issuecomment-114976088.

@mgajdos
Copy link
Contributor

mgajdos commented Jun 24, 2015

mvn clean install -pl core-client -am does it for me. Even on master branch – this line

ServiceLocatorUtilities.enableImmediateScope(result);

causes that ClientRuntimes are not released. Not sure why at the moment.

@mgajdos mgajdos added this to the 2.20 milestone Jul 2, 2015
@mgajdos
Copy link
Contributor

mgajdos commented Jul 2, 2015

The problem is in the test itself. ClientRuntimes are released but not as fast as without the Immediate scope. Please, change the ShutdownHookLeakTest to this:

public class ShutdownHookLeakTest {

    private static final int ITERATIONS = 4000;

    @Test
    public void testShutdownHookDoesNotLeak() throws Exception {
        final Client client = ClientBuilder.newClient();
        final WebTarget target = client.target("http://example.com");

        final Collection shutdownHooks = getShutdownHooks(client);

        for (int i = 0; i < ITERATIONS; i++) {
            // Create/Initialize client runtime.
            target.property("Washington", "Irving")
                    .request()
                    .property("how", "now")
                    .buildGet()
                    .property("Irving", "Washington");
        }

        assertThat(
                "shutdown hook deque size should not copy number of property invocation",
                // 66 % seems like a reasonable threshold for this test to keep it stable
                shutdownHooks.size(), is(lessThan(ITERATIONS * 2 / 3)));
    }

    private Collection getShutdownHooks(final Client client) throws NoSuchFieldException, IllegalAccessException {
        final JerseyClient jerseyClient = (JerseyClient) client;
        final Field shutdownHooksField = JerseyClient.class.getDeclaredField("shutdownHooks");
        shutdownHooksField.setAccessible(true);
        return (Collection) shutdownHooksField.get(jerseyClient);
    }
}

@AdamLindenthal
Copy link
Member

Hi jordmoz, sorry for the delay.
I was actually working on that issue without knowing, that there is a pull request for the same thing, it wasn't linked back here from JIRA.
And surprisingly :) I came to the same result - the same failing test and the same solution - to increase the number of iterations and increase the "instance-tollerance" from 1/2 to 2/3 (which has already been done inbetween).
So, would you please increase the constant to let's say 5000 and commit? You will also probably need to rebase, but than we can finally merge and close the PR.

Thanks,
Adam

@jordmoz
Copy link
Contributor Author

jordmoz commented Sep 8, 2015

Yeah, it seemed like an easy fix :) Writing the test was fun; I didn't know HK2 used a separate thread for the Immediate scoped object instantiations, so my tests kept failing.

Wow, 5000 seems high. Right now it's ... 500? And I think it fails saying it was at 1000. (If memory serves.) So we're increasing it 10x ... I'm fine with this if you are, but perhaps a lower limit would also be acceptable? I haven't touched this in a while, so maybe the limit is different now. I'll work on this again and fix it.

[Update: I misunderstood the difference between iterations and the limit. 5000 is fine. Above test code references 4000 though and that's what I ended up using.]

@jordmoz
Copy link
Contributor Author

jordmoz commented Sep 8, 2015

I took the above test code from @mgajdos and the test now passes. I also rebased everything to current jersey master.

@AdamLindenthal
Copy link
Member

Hi jordmoz,

right now it was 1000, not sure what the number was when Michal suggested 4000. I'll retrigger the verification build and we will see. The thing is, of course for testing it would be great to have lower number of iterations, but for "some" reason (the reason being, that the test is not really deterministic), it kept failing when I tried the same change as you are suggesting locally.

At least on my JVM/configuration/environment, seems like the lower amount of iterations did not "force" the GC to work. Of course the solution is not systematic, is just to make it work and not force you to fix test not related to your patch.

Regards,
Adam

@AdamLindenthal
Copy link
Member

Jenkins, please test this patch.

@jordmoz
Copy link
Contributor Author

jordmoz commented Sep 8, 2015

Test still fails :(

shutdown hook deque size should not copy number of property invocation
Expected: is a value less than <2666>
     but: <4000> was greater than <2666>
Stacktrace

java.lang.AssertionError: shutdown hook deque size should not copy number of property invocation
Expected: is a value less than <2666>
     but: <4000> was greater than <2666>
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
    at org.junit.Assert.assertThat(Assert.java:956)
    at org.glassfish.jersey.client.ShutdownHookLeakTest.testShutdownHookDoesNotLeak(ShutdownHookLeakTest.java:82)

@jordmoz
Copy link
Contributor Author

jordmoz commented Sep 8, 2015

It does pass locally with

mvn clean install -pl core-client -am

which was failing before.

@AdamLindenthal
Copy link
Member

I see. The test needs to be changed. The patch definitely influences the test pass rate, but apparently, as it does not fail everytime (and sometime it fails with the number of instances higher than the treshold, but lower than the number of iterations), I guess we can assume that it does not cause a new leak.

For now, I'll just try to re-run the build, in the meantime I'll try to think of what to do with the test.

Cheers,
Adam

@AdamLindenthal
Copy link
Member

Jenkins, please re-test this patch.

@AdamLindenthal
Copy link
Member

Jenkins, please re-test this patch.

@jordmoz
Copy link
Contributor Author

jordmoz commented Sep 17, 2015

Frustrating. :(

@AdamLindenthal
Copy link
Member

Indeed. The test is not really deterministic, however I didn't find with any better idea how to test the change it tests.

Just spent some time experimenting with it and came up with one more desperate try, counting instances not enqueued to the reference queue and/or references, that still point to a live object. This reduces the total number, at least on my environment. Hard to predict, how it will behave on Jenkins though.

Could you please try to change the assert to the following code:

        System.gc();

        int notEnqueued = 0;
        int notNull = 0;
        for (Object o : shutdownHooks) {
            if (((WeakReference<JerseyClient.ShutdownHook>) o).get() != null) {
                notNull++;
            }
            if (!((WeakReference<JerseyClient.ShutdownHook>) o).isEnqueued()) {
                notEnqueued++;
            }
        }

        assertThat(
                "Non-null shutdown hook references count should not copy number of property invocation",
                // 66 % seems like a reasonable threshold for this test to keep it stable
                notNull, is(lessThan(THRESHOLD)));

        assertThat(
                "Shutdown hook references count not enqueued in the ReferenceQueue should not copy number of property invocation",
                // 66 % seems like a reasonable threshold for this test to keep it stable
                notEnqueued, is(lessThan(THRESHOLD)));

I hope that this will make the test more stable, but without trying it on Jenkins, we will never know...

Regards,
Adam

@jordmoz
Copy link
Contributor Author

jordmoz commented Sep 18, 2015

I rebased onto jersey master and put in your patch. Please try jenkins again.

@jordmoz
Copy link
Contributor Author

jordmoz commented Sep 18, 2015

Hrm I seem to be missing a client.close(), which is present in the master version. Is that not needed? I think a previous patch may have taken it out. The test does pass for me when running:

mvn clean install -pl core-client -am

@AdamLindenthal
Copy link
Member

Jenkins, please test this patch.

@AdamLindenthal
Copy link
Member

Thanks. Well, I guess the test is not necessary, but I would think it will be merged anyway. I hope this time the test passes also on Jenkins, the previous version did pass on my environment as well, but the VM on Jenkins seems to be more lazy about GCing.

Adam

@AdamLindenthal
Copy link
Member

Wow, nice! :-)

AdamLindenthal pushed a commit that referenced this pull request Sep 18, 2015
JERSEY-2291: Enable HK2 Immediate scope to support HK2 @Immediate services.
@AdamLindenthal AdamLindenthal merged commit 26d0f15 into javaee:master Sep 18, 2015
@AdamLindenthal
Copy link
Member

Thanks for the contribution and sorry for hassle with the failing test.

Cheers,
Adam

@jordmoz
Copy link
Contributor Author

jordmoz commented Sep 18, 2015

Crazy. Thanks for all your help!

AdamLindenthal pushed a commit that referenced this pull request Sep 21, 2015
Change-Id: I8c7d298816ebe51dc9f76450ae2f32096f91a999
mpotociar pushed a commit that referenced this pull request Sep 21, 2015
@AdamLindenthal AdamLindenthal modified the milestones: 2.22, 2.20 Sep 23, 2015
@stepanv
Copy link
Contributor

stepanv commented Oct 6, 2015

Hi, I'm sorry but we have to revert your change. The reason is that HK2 does not work as expected and a memory leak can occur under certain circumstances. Tracked here https://java.net/jira/browse/HK2-280.

@jordmoz
Copy link
Contributor Author

jordmoz commented Oct 7, 2015

Argh. Thanks for the update.

@stepanv
Copy link
Contributor

stepanv commented Nov 16, 2015

Now, when https://java.net/jira/browse/HK2-280 is solved. It might be possible to make it work. If anyone is interested, don't hesitate to get into it. I gave it a quick shoot but without success (https://java.net/jira/browse/JERSEY-2291?focusedCommentId=390642&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-390642 )
Don't forget to also test jersey client when immediate scope services are enabled - we don't want extra thread for every client in a jvm. (See https://github.com/jersey/jersey/blob/master/core-client/src/test/java/org/glassfish/jersey/client/ShutdownHookLeakTest.java which I just enabled (was disabled because of this PR)).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants