Skip to content

Commit 21b4a51

Browse files
authored
Add synchronization around use of JDT code formatter to prevent NPE/race condition (#6079)
* Add synchronization around use of JDT code formatter to prevent NPE/race condition * Add changelog
1 parent a5a6de8 commit 21b4a51

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java V2",
4+
"contributor": "",
5+
"description": "Add synchronization around use of JDT code formatter to prevent NPE/race condition during code generation."
6+
}

codegen-lite/src/main/java/software/amazon/awssdk/codegen/lite/emitters/JavaCodeFormatter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
public class JavaCodeFormatter implements CodeTransformer {
3333

3434
private static final Map<String, Object> DEFAULT_FORMATTER_OPTIONS;
35+
private static final Object lock = new Object();
3536

3637
static {
3738
DEFAULT_FORMATTER_OPTIONS = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
@@ -89,10 +90,16 @@ public JavaCodeFormatter(final Map<String, Object> overrideOptions) {
8990

9091
@Override
9192
public String apply(String contents) {
92-
TextEdit edit = codeFormatter.format(
93+
TextEdit edit;
94+
// There is a race condition in the org.eclipse.jdt.internal.formatter.DefaultCodeFormatter in version 3.10.0.
95+
// The static PROBING_SCANNER can have its state changed by multiple threads.
96+
// Synchronize our usage of that class to ensure we don't hit this.
97+
synchronized (lock) {
98+
edit = codeFormatter.format(
9399
CodeFormatter.K_COMPILATION_UNIT
94100
| CodeFormatter.F_INCLUDE_COMMENTS, contents, 0,
95101
contents.length(), 0, System.lineSeparator());
102+
}
96103

97104
if (edit == null) {
98105
// TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost

codegen/src/main/java/software/amazon/awssdk/codegen/emitters/JavaCodeFormatter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
public class JavaCodeFormatter implements CodeTransformer {
3434

3535
private static final Map<String, Object> DEFAULT_FORMATTER_OPTIONS;
36+
private static final Object lock = new Object();
3637

3738
static {
3839
DEFAULT_FORMATTER_OPTIONS = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
@@ -90,10 +91,16 @@ public JavaCodeFormatter(final Map<String, Object> overrideOptions) {
9091

9192
@Override
9293
public String apply(String contents) {
93-
TextEdit edit = codeFormatter.format(
94+
TextEdit edit;
95+
// There is a race condition in the org.eclipse.jdt.internal.formatter.DefaultCodeFormatter in version 3.10.0.
96+
// The static PROBING_SCANNER can have its state changed by multiple threads.
97+
// Synchronize our usage of that class to ensure we don't hit this.
98+
synchronized (lock) {
99+
edit = codeFormatter.format(
94100
CodeFormatter.K_COMPILATION_UNIT
95101
| CodeFormatter.F_INCLUDE_COMMENTS, contents, 0,
96102
contents.length(), 0, Constant.LF);
103+
}
97104

98105
if (edit == null) {
99106
// TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost

0 commit comments

Comments
 (0)