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

[java] Fix intermittent NPE ClassSub.enclosingInfo is null #4834

Merged
merged 4 commits into from Mar 11, 2024

Conversation

adangel
Copy link
Member

@adangel adangel commented Feb 24, 2024

Describe the PR

Partial fix for #4757

ClassStub.enclosingInfo might indeed be null, when other threads want to use it. This is the case for stacktraces 1a, 1b and the first in 2.

Not yet solved: why is GenericSigBase.LazyClassSignature#superItfs null - which causes the NPE in TypeOps.mapPreservingSelf(TypeOps.java:951)...

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@adangel adangel added this to the 7.0.0 milestone Feb 24, 2024
LOG.error(t.toString(), t);
finishParse(true);
}

// the status must be updated as last statement, so that
Copy link
Member Author

Choose a reason for hiding this comment

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

I think, this is the reason why other threads might see ClassStub.enclosingInfo as null - it is set by finishParse() (at least for simple top level classes), but finishParse() was called after the status was set FULL (finished). That means, another thread wouldn't have waited until finishParse() is finished... I could reproduce this problem by adding a Thread.yield() or a Thread.sleep() at the beginning of finishParse(), just before enclosingInfo is set.

Copy link
Member Author

Choose a reason for hiding this comment

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

Update: a Thread.sleep() works better to reproduce the exceptions.

// e.g. in order to determine "annotAttributes", getDeclaredMethods() is called, which
// calls ensureParsed().
// Note: Other threads can't reenter, since our thread own the ParseLock monitor.
return true;
Copy link
Member Author

Choose a reason for hiding this comment

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

That's a consequence that we set the status of the ParseLock to FULL later - finishParse() actually calls back into ensureParsed() and tries to do the parsing again. Since we are in the same thread, we already own the monitor and actually reenter the ParseLock - but the status is already "BEING_PARSED".

I think, for ClassStub it's ok to allow reenter without being picky. The only field, that is being finished in finishParse is annotAttributes - everything else is already in set.

@@ -163,7 +163,7 @@ protected boolean isGeneric() {

@Override
protected boolean postCondition() {
return (superItfs != null && superType != null || signature == null) && typeParameters != null;
return superItfs != null && (superType != null || signature == null) && typeParameters != null;
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't figure out yet, why superItfs seems to end up as null. As far as I see, it is always initialized by the signature parser.

The superType can indeed be null - e.g. if we have java.lang.Object - and that can only happen, if signature == null. So I think, the parentheses are wrong. But I doubt it makes a big difference.

Copy link
Member Author

Choose a reason for hiding this comment

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

superItfs ends up being null, if the doParse() call is aborted with a NPE - then we never set superItfs or superType.
I've also seen the postCondition failed when I reproduced the bug with Thread.sleep.

@pmd-test
Copy link

pmd-test commented Feb 24, 2024

1 Message
📖 No regression tested rules have been changed.
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact

Generated by 🚫 Danger

LOG.error(t.toString(), t);
finishParse(true);
}

// the status must be updated as last statement, so that
Copy link
Member Author

Choose a reason for hiding this comment

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

Update: a Thread.sleep() works better to reproduce the exceptions.

finishParse(!success);
} catch (Throwable t) {
status = ParseStatus.FAILED;
this.status = status;
LOG.error(t.toString(), t);
LOG.error("Parsing failed in ParseLock#doParse()", t);
Copy link
Member Author

Choose a reason for hiding this comment

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

Figured out the 2nd stacktrace of #4757 now, the case when GenericSigBase#supertIfs is null:

So, the issue that superIfs is null, is a consequential error of the earlier NPE and both should be fixed now.

At least, I couldn't reproduce them anymore with Thread.sleep.

@adangel adangel marked this pull request as ready for review February 25, 2024 15:53
@jsotuyod jsotuyod merged commit 7bdc0b2 into pmd:master Mar 11, 2024
3 checks passed
@adangel adangel deleted the issue-4757-npe branch March 11, 2024 21:27
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.

[java] Intermittent NPEs while analyzing Java code
3 participants