Skip to content

Commit

Permalink
[core] Ensure the dht node cahe is written safely (#501)
Browse files Browse the repository at this point in the history
Now it's threadsafe when users call StartAsync/StopAsync on multiple different TorrentManager instances concurrently.

Fixes #499
  • Loading branch information
alanmcgovern authored Jan 13, 2022
1 parent 3e91b14 commit d489f31
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ public async Task SaveStateAsync (string pathToStateFile)

#region Member Variables

readonly SemaphoreSlim dhtNodeLocker;

readonly ListenManager listenManager; // Listens for incoming connections and passes them off to the correct TorrentManager
int tickCount;
/// <summary>
Expand Down Expand Up @@ -269,6 +271,7 @@ public ClientEngine (EngineSettings settings)
CheckSettingsAreValid (Settings);

allTorrents = new List<TorrentManager> ();
dhtNodeLocker = new SemaphoreSlim (1, 1);
publicTorrents = new List<TorrentManager> ();
Torrents = new ReadOnlyCollection<TorrentManager> (publicTorrents);

Expand Down Expand Up @@ -823,12 +826,19 @@ async ReusableTasks.ReusableTask MaybeSaveDhtNodes ()
if (nodes.Length == 0)
return;

await Task.Run (() => {
// Perform this action on a threadpool thread.
await MainLoop.SwitchThread ();

// Ensure only 1 thread at a time tries to save DhtNodes.
// Users can call StartAsync/StopAsync many times on
// TorrentManagers and the file write could happen
// concurrently.
using (await dhtNodeLocker.EnterAsync ().ConfigureAwait (false)) {
var savePath = Settings.GetDhtNodeCacheFilePath ();
var parentDir = Path.GetDirectoryName (savePath);
Directory.CreateDirectory (parentDir);
File.WriteAllBytes (savePath, nodes);
});
}
}

public async Task UpdateSettingsAsync (EngineSettings settings)
Expand Down

0 comments on commit d489f31

Please sign in to comment.