Skip to content

Commit 698e4bd

Browse files
committed
fix threading issue with symlink resolution
1 parent d2394fb commit 698e4bd

File tree

1 file changed

+2
-32
lines changed

1 file changed

+2
-32
lines changed

Sources/TSCBasic/FileSystem.swift

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -528,19 +528,8 @@ public class InMemoryFileSystem: FileSystem {
528528
}
529529
}
530530

531-
// Used to ensure that DispatchQueues are releassed when they are no longer in use.
532-
private struct WeakReference<Value: AnyObject> {
533-
weak var reference: Value?
534-
535-
init(_ value: Value?) {
536-
self.reference = value
537-
}
538-
}
539-
540531
/// The root filesytem.
541532
private var root: Node
542-
/// A map that keeps weak references to all locked files.
543-
private var lockFiles = Dictionary<AbsolutePath, WeakReference<DispatchQueue>>()
544533
/// Used to access lockFiles in a thread safe manner.
545534
private let lockFilesLock = Lock()
546535

@@ -841,27 +830,8 @@ public class InMemoryFileSystem: FileSystem {
841830
}
842831

843832
public func withLock<T>(on path: AbsolutePath, type: FileLock.LockType = .exclusive, _ body: () throws -> T) throws -> T {
844-
let fileQueue: DispatchQueue = try lockFilesLock.withLock {
845-
846-
let resolvedPath: AbsolutePath
847-
848-
// FIXME: resolving symlinks is not yet thread safe
849-
if case let .symlink(destination) = try getNode(path)?.contents {
850-
resolvedPath = AbsolutePath(destination, relativeTo: path.parentDirectory)
851-
} else {
852-
resolvedPath = path
853-
}
854-
855-
if let queueReference = lockFiles[resolvedPath], let queue = queueReference.reference {
856-
return queue
857-
} else {
858-
let queue = DispatchQueue(label: "org.swift.swiftpm.in-memory-file-system.file-queue", attributes: .concurrent)
859-
lockFiles[resolvedPath] = WeakReference(queue)
860-
return queue
861-
}
862-
}
863-
864-
return try fileQueue.sync(flags: type == .exclusive ? .barrier : .init() , execute: body)
833+
// FIXME: Lock individual files once resolving symlinks is thread-safe.
834+
return try lockFilesLock.withLock(body)
865835
}
866836
}
867837

0 commit comments

Comments
 (0)