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

Enable Add-Content to share read access to other tools while writing content #8091

Merged

Conversation

SteveL-MSFT
Copy link
Member

@SteveL-MSFT SteveL-MSFT commented Oct 20, 2018

PR Summary

Add-Content uses a writer that currently sets share access to write. So if using a tool like tail to read a log file while trying to write to it will fail. Fix is to change share access to readwrite which will allow other processes to continue to read from it while the writer writes new content.

Fix #5924

PR Checklist

change ContentWriter to use shared read and write access
@@ -59,3 +59,37 @@ Describe "Add-Content cmdlet tests" -Tags "CI" {
}
}
}

Describe "Add-Content feature tests" -Tag Feature {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We always have problems with async tests. Do we really need the test? I'd only add an comment in C# code.

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 kind of agree on async tests being inherently fragile. Perhaps for this one a comment in the code is sufficient.

Copy link
Contributor

@anmenaga anmenaga Oct 22, 2018

Choose a reason for hiding this comment

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

Can make it sync:

  1. open a file with FileStream(FileAccess.Read, FileShare.ReadWrite) and keep it open.
  2. run new Add-Content (with FileAccess.Write, FileShare.ReadWrite)
  3. close FileStream from 1). The test should not throw.

The old Add-Content (with FileShare.Write) in 2) would throw.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good suggestion, let me see if that repros the original issue.

Copy link
Member

Choose a reason for hiding this comment

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

Please see if these changes are possible. It would harden the test.

Copy link
Member Author

Choose a reason for hiding this comment

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

@anmenaga that worked perfectly and so simple :)

$writer = Start-Job -ScriptBlock {
param($logpath)

1..10 | % {
Copy link
Member

Choose a reason for hiding this comment

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

Use ForEach-Object instead of %

Choose a reason for hiding this comment

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

is (1..10).foreach{...} faster then ForEach-Object?

@adityapatwardhan adityapatwardhan merged commit 082c3b0 into PowerShell:master Oct 23, 2018
@SteveL-MSFT SteveL-MSFT deleted the content-filesharereadwrite branch October 24, 2018 00:06
adityapatwardhan pushed a commit to adityapatwardhan/PowerShell that referenced this pull request Apr 9, 2019
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.

Why does Add-Content require a read lock when appending to files?
5 participants