Add synchronization around use of JDT code formatter to prevent NPE/race condition #6079
+22
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add synchronization around use of JDT code formatter to prevent NPE/race condition.
Motivation and Context
Occasionally a NullPointerException (NPE) is raised during code generation with stack traces point to
the JavaCodeFormatte#L93r. The NPE is happening deep in the eclipse JDT code (in the scanner created by the DefaultCodeFormatter).
It appears there is a race condition in the
org.eclipse.jdt.internal.formatter.DefaultCodeFormatter
in theprobeFormatting
method (see the stack trace). The decompiled .class file has this code (simplified to highlight issue):Since the
PROBING_SCANNER
is static and shared across all instances of the class, its possible for one thread to complete the work and callsetSource(null)
while another thread is in the working section and trying to callgetNextToken()
, which will fail with the NPE we are seeing in stack traces with the message:Cannot load from char array because "this.source" is null
.We currently depend on org.eclipse.jdt.core at 3.10.0 which is ~10 years old, but we cannot easily upgrade this dependency - newer versions of the library require later versions of the JDK.