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

fix: potential race condition on reloading task tag config #533

Merged
merged 1 commit into from
May 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,25 @@ public final class MarkerUtils {
private static final String PROBLEMMARKER_TYPE = "org.eclipse.tm4e.ui.problemmarker";
private static final String TASKMARKER_TYPE = "org.eclipse.tm4e.ui.taskmarker";

private static final Map<String, MarkerConfig> MARKERCONFIG_BY_TAG = new HashMap<>();
private static Pattern TAG_SELECTOR_PATTERN = lazyNonNull();
private static final class MarkerConfigs {
final Map<String, MarkerConfig> markerConfigByTag = new HashMap<>();
final Pattern tagSelectorPattern;

static {
reloadMarkerConfigs();
}

public static void reloadMarkerConfigs() {
synchronized (MARKERCONFIG_BY_TAG) {
MARKERCONFIG_BY_TAG.clear();
MarkerConfigs() {
for (final var markerConfig : PreferenceHelper.loadMarkerConfigs()) {
MARKERCONFIG_BY_TAG.put(markerConfig.tag, markerConfig);
markerConfigByTag.put(markerConfig.tag, markerConfig);
}
TAG_SELECTOR_PATTERN = Pattern
.compile("\\b(" + MARKERCONFIG_BY_TAG.keySet().stream().collect(Collectors.joining("|")) + ")\\b");
tagSelectorPattern = Pattern
.compile("\\b(" + markerConfigByTag.keySet().stream().collect(Collectors.joining("|")) + ")\\b");
}
}

private static volatile MarkerConfigs MARKER_CONFIGS = new MarkerConfigs();

public static synchronized void reloadMarkerConfigs() {
MARKER_CONFIGS = new MarkerConfigs();
}

/**
* Updates all TM4E text markers of the corresponding document starting from
* <code>event.ranges.get(0).fromLineNumber</code> until the end of the document.
Expand Down Expand Up @@ -107,6 +108,10 @@ private static void updateTextMarkers(final TMDocumentModel docModel, final int
markersOfLine.add(marker);
}

final var markerConfigs = MARKER_CONFIGS;
final var markerConfigByTag = markerConfigs.markerConfigByTag;
final var tagSelectorPattern = markerConfigs.tagSelectorPattern;

// iterate over all lines
for (int lineNumber = startLineNumber; lineNumber <= numberOfLines; lineNumber++) {
final var lineNumberObj = Integer.valueOf(lineNumber);
Expand All @@ -131,11 +136,11 @@ private static void updateTextMarkers(final TMDocumentModel docModel, final int
if (commentText.length() < 3)
continue;

final var matcher = MarkerUtils.TAG_SELECTOR_PATTERN.matcher(commentText);
final var matcher = tagSelectorPattern.matcher(commentText);
if (!matcher.find())
continue;

final var markerConfig = MarkerUtils.MARKERCONFIG_BY_TAG.get(matcher.group(1));
final var markerConfig = markerConfigByTag.get(matcher.group(1));
final var markerText = commentText.substring(matcher.start()).trim();

final var attrs = new HashMap<String, Object>();
Expand All @@ -149,7 +154,7 @@ private static void updateTextMarkers(final TMDocumentModel docModel, final int
attrs.put(IMarker.SOURCE_ID, "TM4E");

// only create a new marker if no matching marker already exists
String markerTypeId = switch (markerConfig.type) {
final String markerTypeId = switch (markerConfig.type) {
case PROBLEM -> PROBLEMMARKER_TYPE;
case TASK -> TASKMARKER_TYPE;
};
Expand Down