[Bug] StreamProvider.CreateStreamAsync garbage output due to memory cache
See original GitHub issueI’m using MonoTorrent 2.0-rev0017
with StreamProvider
and CreateStreamAsync
.
The stream doesn’t guarantee that bytes read from disk actually have been written.
https://github.com/alanmcgovern/monotorrent/blob/master/src/MonoTorrent/MonoTorrent.Streaming/LocalStream.cs#L113 This waits and switches on the availability bit (piece downloaded).
https://github.com/alanmcgovern/monotorrent/blob/master/src/MonoTorrent/MonoTorrent.Client.PieceWriters/DiskWriter.cs#L146 This buffers writes in-memory for performance. Therefore, no flush to disk until a threshold is passed.
This seems to result in the LocalStream
reading while the DiskWriter
hasn’t flushed (all) data yet.
Therefore the stream is unreliable and produces garbage data, at times.
I can see two solutions right now:
Disable the cache Even then, there’s still an error window because read/write isn’t synchronized. If a read request request occurs during the window in which the writer is still writing, the reader will produce garbage, too. This makes me conclude that the same issue exists in older versions of monotorrent, except it’s less pronounced, because there’s less time that data isn’t available on-disk.
Synchronize reader to the writer This is the robust option. The local stream should be hooked into the writer, and only be able to read data that has been flushed to disk or is available in memory. This is also the most complex option.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (7 by maintainers)
Top GitHub Comments
Seems to work just fine!
I’ve added tests covering the new cases and merged it all to master.
I’ll issue a new alpha release next week I’d say, so if you have time to test a master build in the meantime please do!