Skip to content

[Az.Accounts] Use mutex to prevent cross process file I/O #12281

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

Merged
merged 4 commits into from
Jun 30, 2020

Conversation

isra-fel
Copy link
Member

@isra-fel isra-fel commented Jun 29, 2020

Description

See #9448 for more info.

I also found another lock that needs to be replaced with Mutex: https://github.com/Azure/azure-powershell/blob/master/src/Accounts/Authentication.ResourceManager/ProtectedFileProvider.cs#L90
The other locks are safe.

I tested using Peter's sample code and it worked.

Checklist

  • I have read the Submitting Changes section of CONTRIBUTING.md
  • The title of the PR is clear and informative
  • The appropriate ChangeLog.md file(s) has been updated:
    • For any service, the ChangeLog.md file can be found at src/{{SERVICE}}/{{SERVICE}}/ChangeLog.md
    • A snippet outlining the change(s) made in the PR should be written under the ## Upcoming Release header -- no new version header should be added
  • The PR does not introduce breaking changes
  • If applicable, the changes made in the PR have proper test coverage
  • For public API changes to cmdlets:
    • a cmdlet design review was approved for the changes in this repository (Microsoft internal only)
    • the markdown help files have been regenerated using the commands listed here

@adxsdkps
Copy link
Collaborator

Can one of the admins verify this patch?

@@ -87,18 +92,17 @@ public StreamWriter CreateWriter()

protected virtual void InitializeStream()
{
lock (_initializationLock)
_initializationLock.WaitOne();
Copy link
Member Author

Choose a reason for hiding this comment

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

I believe we should use Mutex here, because TryGetStreamLock() may open files for write exclusively, see https://github.com/Azure/azure-powershell-common/blob/master/src/Authentication.Abstractions/DiskDataStore.cs#L324

@isra-fel isra-fel marked this pull request as ready for review June 29, 2020 08:09
@isra-fel isra-fel changed the title use mutex to prevent cross process writing to token cache file [Az.Accounts] Use mutex to prevent cross process file I/O Jun 29, 2020
erich-wang
erich-wang previously approved these changes Jun 29, 2020
Copy link
Member

@erich-wang erich-wang left a comment

Choose a reason for hiding this comment

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

Sign off with minor comments

}
_initializationLock.ReleaseMutex();
Copy link
Member

Choose a reason for hiding this comment

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

how about put _initializationLock.ReleaseMutex() in finally block?

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. Done

}
fileLock.ReleaseMutex();
Copy link
Member

Choose a reason for hiding this comment

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

suggest to put into finally block

@isra-fel
Copy link
Member Author

/azp run

@azure-pipelines
Copy link
Contributor

Azure Pipelines successfully started running 3 pipeline(s).

@isra-fel isra-fel merged commit 2e8d288 into Azure:master Jun 30, 2020
@isra-fel isra-fel deleted the mutex branch June 30, 2020 02:56
litchiyangMSFT pushed a commit to litchiyangMSFT/azure-powershell that referenced this pull request Aug 4, 2020
* use mutex to prevent cross process writing

to token cache file

* prevent cross-process write to profile file

* change log

* Use "Local" mutex; put ReleaseMutex() in finally
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants