Skip to content

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

Merged
Aditya Patwardhan (adityapatwardhan) merged 2 commits into
PowerShell:masterfrom
SteveL-MSFT:content-filesharereadwrite
Oct 23, 2018
Merged

Aditya Patwardhan (adityapatwardhan) merged 2 commits into
PowerShell:masterfrom
SteveL-MSFT:content-filesharereadwrite

Conversation

@SteveL-MSFT

@SteveL-MSFT Steve Lee (SteveL-MSFT) commented Oct 20, 2018 •

Copy link
Copy Markdown
Member

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
}
}

Describe "Add-Content feature tests" -Tag Feature {

Copy link
Copy Markdown
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
Copy Markdown
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.

@anmenaga Andrew (anmenaga) Oct 22, 2018 •

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Andrew (@anmenaga) that worked perfectly and so simple :)

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

1..10 | % {

Copy link
Copy Markdown
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 %

Copy link
Copy Markdown

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
Aditya Patwardhan (adityapatwardhan) merged commit 082c3b0 into PowerShell:master Oct 23, 2018
@SteveL-MSFT
Steve Lee (SteveL-MSFT) deleted the content-filesharereadwrite branch October 24, 2018 00:06
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Shubham Jain (slayerjain) added a commit to keploy/keploy that referenced this pull request Oct 8, 2026
…ows script tests wait for

precheck-windows' background cases start ensure-docker.ps1 in another
PowerShell process, append each line it prints to a log with Add-Content, and
poll that log every 200 ms. Windows PowerShell 5.1's Add-Content opens the file
with FileShare.Write (PowerShell 6.2 changed it, PowerShell/PowerShell#8091),
so an append that meets the case's read fails with "being used by another
process" on a stderr nobody reported, and the line is lost. When it is the line
the case waits for, the case waits out its deadline: 7 failures since
2026-10-02, all "did not say it was waiting within N s", each at its deadline.

Every writer of a file a case polls now appends through Add-Line, which shares
the file with readers and reports a failed write without throwing, so a lost
diagnostic line can never change the fake docker's answer. Failing background
cases now report the process's stderr, and the cases that wait on another
runner's marker also report the docker calls made and the marker lookups. No
deadline, assertion or case changed.

Signed-off-by: slayerjain <shubhamkjain@outlook.com>
Shubham Jain (slayerjain) added a commit to keploy/keploy that referenced this pull request Oct 8, 2026
…ows script tests wait for (#4725)

precheck-windows' background cases start ensure-docker.ps1 in another
PowerShell process, append each line it prints to a log with Add-Content, and
poll that log every 200 ms. Windows PowerShell 5.1's Add-Content opens the file
with FileShare.Write (PowerShell 6.2 changed it, PowerShell/PowerShell#8091),
so an append that meets the case's read fails with "being used by another
process" on a stderr nobody reported, and the line is lost. When it is the line
the case waits for, the case waits out its deadline: 7 failures since
2026-10-02, all "did not say it was waiting within N s", each at its deadline.

Every writer of a file a case polls now appends through Add-Line, which shares
the file with readers and reports a failed write without throwing, so a lost
diagnostic line can never change the fake docker's answer. Failing background
cases now report the process's stderr, and the cases that wait on another
runner's marker also report the docker calls made and the marker lookups. No
deadline, assertion or case changed.

Signed-off-by: slayerjain <shubhamkjain@outlook.com>
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