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 Kotlin compiler may be released not in all cases. #1088

Merged
Show file tree
Hide file tree
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 @@ -111,16 +111,16 @@ public object KtLint {
}
} catch (e: Exception) {
val (line, col) = preparedCode.positionInTextLocator(node.startOffset)
kotlinPsiFileFactory.releasePsiFileFactory()
throw RuleExecutionException(line, col, fqRuleId, e)
}
}
}

kotlinPsiFileFactory.releasePsiFileFactory()
errors
.sortedWith { l, r -> if (l.line != r.line) l.line - r.line else l.col - r.col }
.forEach { e -> params.cb(e, false) }

kotlinPsiFileFactory.releasePsiFileFactory()
}

private fun prepareCodeForLinting(
Expand Down Expand Up @@ -324,6 +324,7 @@ public object KtLint {
}
}
} catch (e: Exception) {
kotlinPsiFileFactory.releasePsiFileFactory()
// line/col cannot be reliably mapped as exception might originate from a node not present
// in the original AST
throw RuleExecutionException(0, 0, fqRuleId, e)
Expand Down Expand Up @@ -352,6 +353,7 @@ public object KtLint {
}
} catch (e: Exception) {
val (line, col) = preparedCode.positionInTextLocator(node.startOffset)
kotlinPsiFileFactory.releasePsiFileFactory()
throw RuleExecutionException(line, col, fqRuleId, e)
}
}
Expand All @@ -363,6 +365,7 @@ public object KtLint {
}

if (!mutated) {
kotlinPsiFileFactory.releasePsiFileFactory()
return params.text
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ internal class KotlinPsiFileFactory {
private val acquireCounter = AtomicInteger(0)
private val initializerLock = Any()

internal fun acquirePsiFileFactory(isFromCli: Boolean): PsiFileFactory {
internal fun acquirePsiFileFactory(isFromCli: Boolean): PsiFileFactory = synchronized(initializerLock) {
acquireCounter.incrementAndGet()
return psiFileFactory ?: synchronized(initializerLock) {
initializePsiFileFactory(isFromCli).also {
psiFileFactory = it
}
return psiFileFactory ?: initializePsiFileFactory(isFromCli).also {
psiFileFactory = it
}
}

internal fun releasePsiFileFactory() {
internal fun releasePsiFileFactory() = synchronized(initializerLock) {
val acquiredCount = acquireCounter.decrementAndGet()
if (acquiredCount == 0) synchronized(initializerLock) {
if (acquiredCount == 0) {
tempFiles?.toFile()?.deleteRecursively()
tempFiles = null
psiFileFactory = null
Expand Down