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

[WIP] experiment - Use SemaphoreSlim to prevent concurrent basestream access #619

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Numpsy
Copy link
Contributor

@Numpsy Numpsy commented May 6, 2021

A spin off / follow up to #589 which tries to use SemaphoreSlim to prevent concurrent access to the base stream instead of lock()

Another followup thought after looking at the bits that lock a bit more:

The TestArchive function:

  1. calls TestLocalHeader, which locks the stream for the duration of the operation
  2. reads the stream content via GetInputStream, which returns the locking PartialInputStream
  3. uses ZipHelperStream to read the data descriptor, which I don't think does any locking

so might (3) be a hole where multiple things could read the stream at once?

I certify that I own, and have sufficient rights to contribute, all source code and related material intended to be compiled or integrated with the source code for the SharpZipLib open source product (the "Contribution"). My Contribution is licensed under the MIT License.

@codecov
Copy link

codecov bot commented May 6, 2021

Codecov Report

Merging #619 (215f747) into master (1b9fcfc) will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #619      +/-   ##
==========================================
+ Coverage   73.35%   73.40%   +0.05%     
==========================================
  Files          68       68              
  Lines        8718     8735      +17     
==========================================
+ Hits         6395     6412      +17     
  Misses       2323     2323              
Impacted Files Coverage Δ
src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs 78.06% <100.00%> (+0.25%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1b9fcfc...215f747. Read the comment docs.

{
baseStream_.Dispose();
this.baseStreamSemaphore.Wait();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this the other day and wasn't sure about it, and it still feels off - if the idea of the existing lock is to prevent the stream being disposed while being read (rather than 'just' being disposed in between reads) then that needs to be maintained, but then you might also need to avoid disposing the semaphore while another thread is using it, which just makes things more complicated.

Still not sure if the lock is doing anything other than trying to save users from their own mistakes though (preventing an explicit attempt to dispose the archive while still using it) :-(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not even sure that that is something we should try to manage. Tried looking up when the locking was added, but it lead me to this wonderful commit with 124,411 additions and 10,927 deletions... Great.

@Numpsy
Copy link
Contributor Author

Numpsy commented May 6, 2021

I've read elsewhere that you don't need to dispose the semaphore if you aren't using it's wait handle property directly, but I don't know if that's something to depend on.

Extra related question: Should the Async stream calls use ConfigureAwait ?

@piksel
Copy link
Member

piksel commented May 8, 2021

Regarding ConfigureAwait:

if you’re writing general-purpose library code, use ConfigureAwait(false)

from https://devblogs.microsoft.com/dotnet/configureawait-faq/#when-should-i-use-configureawaitfalse

@Numpsy
Copy link
Contributor Author

Numpsy commented May 8, 2021

Yes, just thought i'd check as it can mean adding it in many places, and #574 didn't seem to do it.

@piksel
Copy link
Member

piksel commented May 11, 2021

No, but that's just my bad 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants