question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Powershell crashes microsoft/powershell docker container

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
daxian-dbwcommented, Sep 16, 2021

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.

1reaction
JohnLBevancommented, Jan 22, 2021

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 

Read more comments on GitHub >

github_iconTop Results From Across the Web

Powershell crashes microsoft/powershell docker container
When running docker run -itd to start the powershell container, since the container is detached, System.Console.BufferWidth and System.Console.
Read more >
Docker Container stops after running powershell script
I needed to run a powershell script when container is instantiated. That script will modify a config file with the environment variable value....
Read more >
Troubleshoot Windows containers
Having trouble setting up your machine or running a container? We created a PowerShell script to check for common problems.
Read more >
"docker login" hangs in PowerShell ISE
Expected behavior Prompt for username, password and email when docker login is invoked in the console pane of PowerShell ISE window.
Read more >
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 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found