Skip to content

Commit

Permalink
Fix FileWriter concurrency (#2205)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmishenev committed Oct 29, 2021
1 parent 100947b commit f63134d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions plugins/base/src/main/kotlin/renderers/FileWriter.kt
@@ -1,6 +1,8 @@
package org.jetbrains.dokka.base.renderers

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import org.jetbrains.dokka.plugability.DokkaContext
import java.io.File
Expand All @@ -10,15 +12,12 @@ import java.nio.file.*

class FileWriter(val context: DokkaContext): OutputWriter {
private val createdFiles: MutableSet<String> = mutableSetOf()
private val createdFilesMutex = Mutex()
private val jarUriPrefix = "jar:file:"
private val root = context.configuration.outputDir

override suspend fun write(path: String, text: String, ext: String) {
if (createdFiles.contains(path)) {
context.logger.error("An attempt to write ${root}/$path several times!")
return
}
createdFiles.add(path)
if (checkFileCreated(path)) return

try {
val dir = Paths.get(root.absolutePath, path.dropLastWhile { it != '/' }).toFile()
Expand All @@ -32,6 +31,15 @@ class FileWriter(val context: DokkaContext): OutputWriter {
}
}

private suspend fun checkFileCreated(path: String): Boolean = createdFilesMutex.withLock {
if (createdFiles.contains(path)) {
context.logger.error("An attempt to write ${root}/$path several times!")
return true
}
createdFiles.add(path)
return false
}

override suspend fun writeResources(pathFrom: String, pathTo: String) =
if (javaClass.getResource(pathFrom)?.toURI()?.toString()?.startsWith(jarUriPrefix) == true) {
copyFromJar(pathFrom, pathTo)
Expand Down

0 comments on commit f63134d

Please sign in to comment.