PSReadLine: Powershell crashes microsoft/powershell docker container

Running Docker Edge on MAC OSX 10.12.6 From within iterm2 pulled microsoft/powershell:latest container. spin up the container ok and can docker exec into the container ok

run push in container to start powershell and receive the following output:

root@3d90889506e0:/# pwsh
PowerShell v6.0.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS />
Oops, something went wrong.  Please report this bug with the details below.
Report on GitHub: https://github.com/lzybkr/PSReadLine/issues/new
-----------------------------------------------------------------------
Last 0 Keys:


Exception:
System.DivideByZeroException: Attempted to divide by zero.
   at Microsoft.PowerShell.PSConsoleReadLine.Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)
-----------------------------------------------------------------------

Steps to reproduce

docker pull microsoft/powershell docker run --name=mypowershell -itd microsoft/powershell:latest docker exec -it mypowershell /bin/bash

type pwsh at prompt

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 16 (6 by maintainers)

Most upvoted comments

My investigation shows that the DivideByZeroException exception is caused by having the container detached. When running docker run -itd to start the powershell container, since the container is detached, System.Console.BufferWidth and System.Console.BufferHeight are both 0, meaning the console windows doesn’t exist, and it’s actually a broken state. That leads to the DivideByZeroException.

I don’t think there is anything to fix in PSReadLine. As a workaround, if you need to start the container in a detached mode, then instead of using the default CMD, which is really just for interactive attached usage, just explicitly specify a command to prevent things from shutting down, such as

docker run --name=mypowershell -itd mcr.microsoft.com/powershell:latest pwsh -c "[console]::ReadKey()"

Then, you can run docker exec -it mypowershell /bin/bash or docker exec -it mypowershell pwsh


For the “Remote - Container” extension in VSCode, now the extension automatically adds a docker-compose.yml file to override the default CMD of a container image. The overriding command is just a loop, to prevents the container from shutting down.

    # Overrides default command so things don't shut down after the process ends.
    command: /bin/sh -c "while sleep 1000; do :; done"

So I think you don’t need to do anything extra, and it will just work. If the overriding command is not automatically added, then try adding it by yourself.

The issue seems to be with these lines: https://github.com/PowerShell/PSReadLine/blob/fd1265e385cb4b7b860d8f1452da1ceb347e123e/PSReadLine/Render.cs#L489-L498 i.e. as BufferWidth is zero.

Below’s some additional info (sadly copy-paste is not possible from the Docker for Desktop logs panel, where this info’s available).

### Environment 
PSReadLine: 2.1.0 
PowerShell: 7.1.1 
OS: Linux 4.19.104-microsoft-standard #1 SMP Wed Feb 19 06:37:35 UTC 2020 
BufferWidth: 0 
BufferHeight: 0 
Last 1 Keys 
<?>
### Exception 
System.DivideByZeroException: Attempted to divide by zero. 
at Microsoft.PowerShell.PSConsoleReadLine.PhysicalLineCount(Int32 columns, Boolean isFirstLogicalLine, Int32& lenLastPhysicalLine) 
at Microsoft.PowerShell.PSConsoleReadLine.ReallyRender(RenderData renderData, String defaultColor) 
at Microsoft.PowerShell.PSConsoleReadLine.ForceRender() 
at Microsoft.PowerShell.PSConsoleReadLine.Render() at Microsoft.PowerShell.PSConsoleReadLine.Insert(Chat c) 
at Microsoft.PowerShell.PSConsoleReadLine.SelfInsert(Nullable'l key, Object arg) 
at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(PSKeyInfo key, Dictionary'2 dispatchTable, Boolean ignoreIfNoAction, Object arg) 
at Microsoft.PowerShell.PSConsoleReadLine.InputLoop() 
at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, Enginelntrinsics engineIntrinsics, CancellationToken cancellationToken) 

PS /tmp> PS /tmp> 4Process terminated. The value must be greater than or equal to zero and less than the console's buffer size in that dimension. (Parameter 'top') 
Actual value was -1. 
at System.Environment. FailFast(System. String, System.Exception) 
at Microsoft. PowerShell.UnmanagedPSEntry. Start(System. String[], Int32) 
at Microsoft.PowerShell.ManagedPSEntry.Main(System.String[]) System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension. (Parameter 'top') Actual value was -1. 
at System. Console.SetCursorPosition(Int32 left, Int32 top) 
at System. Console. set_CursorLeft(Int32 value) 
at Microsoft.PowerShell.ConsoleHostUserInterface.ReadLineFromConsole(Boolean endOnTab, String initialContent, Boolean calledFromPipeline, String& restOfLine, ReadLineResult& result) in /PowerShell/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs:line 1796 
at Microsoft.PowerShell.ConsoleHostUserInterface.ReadLine(Boolean endOnTab, String initialContent, ReadLineResult& result, Boolean calledFromPipeline, Boolean transcribeResult) in /PowerShell/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs:line 1431 

Appears to still be a thing in 6.1; I’m seeing the same issue in

FROM microsoft/dotnet:2.1.0-rc1-aspnetcore-runtime-stretch-slim
RUN groupadd -g 999 appuser; useradd -r -u 999 -g appuser appuser

RUN apt-get update -y; \
    apt-get install wget curl gnupg apt-transport-https nano procps ca-certificates -y; \
    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -; \
    sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list'; \
    apt-get update; \
    apt-get install -y powershell; \
USER appuser
ENTRYPOINT [ "pwsh" ]
CMD [ "-File", "initScript.ps1" ]

The initScript.ps1 just unpacks some files before starting a dotnet application.

If I exec -ti into the container I’ll get this error sometimes (just after starting pwsh, without entering anything):

$ pwsh
PowerShell v6.1.0-preview.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /app>
Oops, something went wrong.  Please report this bug with the details below.
Report on GitHub: https://github.com/lzybkr/PSReadLine/issues/new
-----------------------------------------------------------------------
Last 0 Keys:


Exception:
System.DivideByZeroException: Attempted to divide by zero.
   at Microsoft.PowerShell.PSConsoleReadLine.Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)
-----------------------------------------------------------------------

And then if I kill the container and start the exact same image again, everything might work smoothly with no error except Error reading or writing history file '/home/appuser/.local/share/powershell/PSReadLine/ConsoleHost_history.txt': Access to the path '/home/appuser/.local/share/powershell/PSReadLine' is denied. (which doesn’t really concern us). Next restart things might still be fine, maybe the one after that as well, and then suddenly a failure. All running the exact same container with the exact same data in it.