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

Mutex constructor fails with "system cannot open the device or file specified" #1464

Closed
Stephanevg opened this issue Apr 12, 2020 · 14 comments
Closed
Labels
issue-mutex The mutex creation issue on macOS Resolution-Answered Question is answered

Comments

@Stephanevg
Copy link

Stephanevg commented Apr 12, 2020

Hi, Just beeing a good citizen, and doing as the console is asking me to do:

bash-3.2$ pwsh
PowerShell 7.0.0
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.

PS /Users/stephanevg/Code/ansible-lab/Roles>
Oops, something went wrong. Please report this bug with the details below.
Report on GitHub: https://github.com/PowerShell/PSReadLine/issues/new

Environment

PSReadLine: 2.0.0
PowerShell: 7.0.0
OS: Darwin 19.3.0 Darwin Kernel Version 19.3.0: Thu Jan 9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64

Exception

System.IO.IOException: The system cannot open the device or file specified. : 'PSReadLineHistoryFile_1525368525'
   at System.Threading.Mutex.CreateMutexCore(Boolean initiallyOwned, String name, Boolean& createdNew)
   at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name)
   at Microsoft.PowerShell.PSConsoleReadLine.DelayedOneTimeInitialize()
   at Microsoft.PowerShell.PSConsoleReadLine.Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken)
@ghost ghost added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Apr 12, 2020
@Stephanevg
Copy link
Author

Context:

I was testing Pester v5, and this error threw:

PS /Users/stephanevg/Code/PSHTML> $e = invoke-pester -Path ./link.tests.ps1 -CI       
Get-Item: /Users/stephanevg/.local/share/powershell/Modules/Pester/5.0.0/Pester.psm1:793
Line |
 793 |                              $i = Get-Item $p
     |                                   ~~~~~~~~~~~
     | Cannot find path '/Users/stephanevg/Code/PSHTML/link.tests.ps1' because it does not exist.

System.Management.Automation.ParameterBindingValidationException: Cannot bind argument to parameter 'Path' because it is null.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at Invoke-Pester<End>, /Users/stephanevg/.local/share/powershell/Modules/Pester/5.0.0/Pester.psm1: line 798
at <ScriptBlock>, <No file>: line 1
bash-3.2$ pwsh
PowerShell 7.0.0
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.

PS /Users/stephanevg/Code/ansible-lab/Roles> 
Oops, something went wrong.  Please report this bug with the details below.
Report on GitHub: https://github.com/PowerShell/PSReadLine/issues/new

@daxian-dbw daxian-dbw added Needs-More Investigation Initial investigation is done, but need to follow up. and removed Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. labels Apr 20, 2020
@daxian-dbw
Copy link
Member

It makes me sad to see this happen on PSReadLine 2.0.0 ☹️
I saw this reported before and thought #1061 would resolve the issue, but now obviously it didn't.
@Stephanevg does it happen to you consistently? It's super hard for me to get a repro.

@daxian-dbw daxian-dbw changed the title Reporting error Mutex constructor fails with "system cannot open the device or file specified" Apr 21, 2020
@Stephanevg
Copy link
Author

Hi @daxian-dbw
Thanks for asking. Actually, I only had it twice.
Since, I didn't encountered the issue anymore (But I was busy with some other stuff).

This was also while testing a early release of Pester v5 (RC1 to be precise).
It could be that that some of the internals there were not completley polished or so.
Perhaps looking in the code of Pester v5 rc1 and what that -CI did can help. I really had it only when I used that parameter.

@iSazonov
Copy link

Did Pester crash before you get the PSRL exception?

@daxian-dbw
Copy link
Member

Got a report on the same issue by @FiggyRoll:

The issue has returned with the exact same error message

Exception

System.IO.IOException: The system cannot open the device or file specified. : 'PSReadLineHistoryFile_3946503603'
  at System.Threading.Mutex.CreateMutexCore(Boolean initiallyOwned, String name, Boolean& createdNew)
  at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name)
  at Microsoft.PowerShell.PSConsoleReadLine.DelayedOneTimeInitialize()
  at Microsoft.PowerShell.PSConsoleReadLine.Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
  at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken)

Other details from the error

### Environment
PSReadLine: 2.0.0
PowerShell: 7.0.1
OS: Darwin 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64
Last 0 Keys

My mac is up to date and I'm not aware of any additional updates that have been installed in the last few days. This was working up until at least Wednesday last week.

To reproduce I just run pwsh from terminal on my mac. I noticed the issue this morning when opening a script in vscode, and then tested just opening powershell in terminal and the issue appeared.

I have powershell installed using brew, and up until this morning was on 7.0.0, I have upgraded to 7.1.0 this morning using 'brew cask upgrade powershell' but the issue persists. I did also have powershell-preview installed using brew which I have now removed, but still have the error.

UPDATE:
Checking installed updates on my mac and the only changes I can see are these

XProtectPlistConfigData:
 Version: 2121
 Source: Apple
 Install Date: 15/05/2020, 17:41

MRTConfigData:
 Version: 1.60
 Source: Apple
 Install Date: 15/05/2020, 17:46

@FiggyRoll
Copy link

Just for some extra info:

Tried the latest beta version of PSReadline today but the issue still persists

@daxian-dbw
Copy link
Member

@FiggyRoll It sounds this issue happens consistently in your environment (is it?). Could you please do the following to collect some diagnosis information?

  1. The mutex name is used to create a file at path /tmp/.dotnet/shm/<session>/<mutexName>, where <session> is global/local <sessionId>, and <mutexName> is the name minus the session prefix (Global\ or Local\). It's unlikely, but can you please check if /tmp on your mac is mapped to somewhere other than private/tmp?
  2. Please close all pwsh sessions (including the sessions opened by VSCode), then check the permission of the folders /tmp/.dotnet and /tmp/.dotnet/shm, to see if their permissions are 777.
  3. If pwsh successfully starts, you should be able to find a sessionxxxxx folder created in /tmp/.dotnet/shm that contains a file named PSReadLineHistoryFile_3946503603. I assume the mutex creation always fails when you starts pwsh, right? If so, please start pwsh by pwsh -noprofile -noninteractive, which won't load PSReadLine. Then please try $mutex = [System.Threading.Mutex]::new($false, "MyM"). If that succeeds, paste the output of dir /tmp/.dotnet/shm -Recurse, and then run $mutex.Dispose().
  4. If the attempt to create a mutex named MyM succeeded, please try $mutex = [System.Threading.Mutex]::new($false, "PSReadLineHistoryFile_3946503603") just like what PSReadLine does, and see if that fail. If that fails, please get the HResult from the IOException. You can get that IOException instance by looking into $Error[0].Exception.

@FiggyRoll
Copy link

Hi @daxian-dbw

Thanks for the reply, I really appreciate you taking the time to look into this. Going through the steps and found an issue on step 1, which might be the cause of the problems?

There is no /tmp/.dotnet directory on my mac. (/tmp is mapped to private/tmp)

Ive tried step 3 as well just to see what errors I am given and can confirm that PS does load correctly using pwsh -noprofile -noninteractive but then when I run thew following command:
$mutex = [System.Threading.Mutex]::new($false, "MyM") that fails and gives the following error:

MethodInvocationException: Exception calling ".ctor" with "2" argument(s): "The system cannot open the device or file specified. : 'MyM'"

Just for completeness I also attempted the command that PSReadLine does and get the following:

$mutex = [System.Threading.Mutex]::new($false, "PSReadLineHistoryFile_3946503603")

MethodInvocationException: Exception calling ".ctor" with "2" argument(s): "The system cannot open the device or file specified. : 'PSReadLineHistoryFile_3946503603'"

@iSazonov
Copy link

Maybe reference on a commit in dotnet/roslyn#28529 helps.

@daxian-dbw
Copy link
Member

daxian-dbw commented May 20, 2020

@FiggyRoll Thanks for providing the additional info!
I just tried removing the /tmp/.dotnet folder, and then start pwsh, and it turned out /tmp/.dotnet folder is automatically created when starting pwsh.
Can you run ls -la /private and copy the mode of the tmp directory? The mode of tmp directory on my mac is drwxrwxrwt. I suspect that your tmp folder doesn't have the permission for the dotnet runtime to create those folders.

@FiggyRoll
Copy link

FiggyRoll commented May 21, 2020

@daxian-dbw I think you have found the issue! I checked the permissions on /tmp and on /private/tmp just to be thorough and both showed 755 for the permissions.

Did some quick googling just to make sure that the normal permissions should be 1777, couldn't find anything definitive but the general consensus seems to be that is correct. So not sure why the permissions on mine would have changed (had issues with brew a while ago so possibly when fixing that)

Anyway, chmod 777 /private/tmp and chmod +t /private/tmp to fix the permissions and PS now works fine. If I run the $mutex commands you provided above they also complete without issue.

Thanks for your help with this, much appreciated.

@iSazonov
Copy link

More user friendly error message will be great to have.

@daxian-dbw
Copy link
Member

@FiggyRoll Thanks for the confirmation. That's great.

More user friendly error message will be great to have.

Agreed. Opened dotnet/runtime#36823. Hopefully dotnet team can improve the error message.
I will close this issue as resolved.

@brainwipe
Copy link

Please keep closed, this is for others landing here.

This error message is also thrown when /tmp is full. You can check how much is free using:

df -k /tmp

If Available is 0 or Use% is 100, then /tmp is full.

How you then go about cleaning it depends on your use case. To remove any file that's older than 10 days:

sudo find /tmp -type f -atime +10 -delete

Apologies for pinging this thread, it's the top result in Google for the error as a search term.

@daxian-dbw daxian-dbw added the issue-mutex The mutex creation issue on macOS label Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue-mutex The mutex creation issue on macOS Resolution-Answered Question is answered
Projects
None yet
Development

No branches or pull requests

5 participants