Bug 29770 - Mono clears the screen when running apps
Summary: Mono clears the screen when running apps
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: 4.0.0
Hardware: PC Linux
: Low minor
Target Milestone: Untriaged
Assignee: Miguel de Icaza [MSFT]
URL:
: 29767 (view as bug list)
Depends on:
Blocks:
 
Reported: 2015-05-05 16:14 UTC by Brad Wilson
Modified: 2016-11-01 16:53 UTC (History)
7 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Video showing the clear-screen behavior (306.30 KB, video/mp4)
2015-05-05 16:14 UTC, Brad Wilson
Details

Description Brad Wilson 2015-05-05 16:14:00 UTC
Created attachment 11073 [details]
Video showing the clear-screen behavior

I've been able to reproduce this with 3.12, 4.0.1, and the current CI build (mono/20150505135216).

Whenever I run mono to run an app, it clears the screen when running the app. I've seen this on several flavors of Linux (the example video is using Debian 8).

I am ssh'd into the machine in question from a Windows box, using the SSH that ships with git version 1.9.5.msysgit.1, though no other applications that I run on the machine in question exhibit this behavior (only Mono apps).
Comment 1 Rodrigo Kumpera 2015-05-05 16:26:06 UTC
*** Bug 29767 has been marked as a duplicate of this bug. ***
Comment 2 Rodrigo Kumpera 2015-05-06 11:50:43 UTC
Marek?
Comment 3 Marek Safar 2015-05-06 14:10:13 UTC
No idea but does this happen for Hello world app too?
Comment 4 Brad Wilson 2015-05-06 15:53:27 UTC
It appears to be related to the use of color.

This does not clear the screen:

using System;

public class HelloWorld {
  public static void Main() {
    Console.WriteLine("Hello, world!");
  }
}

This does:

using System;

public class HelloWorld {
  public static void Main() {
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Hello, world!");
  }
}
Comment 5 Marek Safar 2015-05-07 15:12:42 UTC
Looks like our SetABackground term info string is not really correct in this case.
Comment 6 Miguel de Icaza [MSFT] 2015-12-24 15:32:45 UTC
Activating the color enables the terminfo based terminal to be used, and this can have side effects like this, depending on the TERM setting and the terminal database on the system.

We would need the terminfo database for this scenario, and the TERM setting.  You can use the "infocmp" command to generate this information.
Comment 7 Warren 2016-02-18 16:01:54 UTC
This is still a problem.

In addition to color properties, even adding a handler to Console.CancelKeyPress causes the screen to clear.

Here is the infocmp output on the affected terminal:

#       Reconstructed via infocmp from file: /lib/terminfo/c/cygwin
cygwin|ansi emulation for Cygwin,
        am, hs, mir, msgr, xon,
        colors#8, it#8, pairs#64,
        acsc=+\020\,\021-\030.^Y0\333`\004a\261f\370g\361h\260j\331k\277l\332m\300n\305o~p\304q\304r\304s_t\303u\264v\301w\302x\263y\363z\362{\343|\330}\234~\376,
        bel=^G, bold=\E[1m, clear=\E[H\E[J, cr=^M, cub=\E[%p1%dD,
        cub1=^H, cud=\E[%p1%dB, cud1=\E[B, cuf=\E[%p1%dC,
        cuf1=\E[C, cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA,
        cuu1=\E[A, dch=\E[%p1%dP, dch1=\E[P, dl=\E[%p1%dM,
        dl1=\E[M, ed=\E[J, el=\E[K, el1=\E[1K, fsl=^G, home=\E[H,
        hpa=\E[%i%p1%dG, ht=^I, ich=\E[%p1%d@, ich1=\E[@,
        il=\E[%p1%dL, il1=\E[L, ind=^J, invis=\E[8m, kb2=\E[G,
        kbs=^H, kcub1=\E[D, kcud1=\E[B, kcuf1=\E[C, kcuu1=\E[A,
        kdch1=\E[3~, kend=\E[4~, kf1=\E[[A, kf10=\E[21~,
        kf11=\E[23~, kf12=\E[24~, kf13=\E[25~, kf14=\E[26~,
        kf15=\E[28~, kf16=\E[29~, kf17=\E[31~, kf18=\E[32~,
        kf19=\E[33~, kf2=\E[[B, kf20=\E[34~, kf3=\E[[C, kf4=\E[[D,
        kf5=\E[[E, kf6=\E[17~, kf7=\E[18~, kf8=\E[19~, kf9=\E[20~,
        khome=\E[1~, kich1=\E[2~, knp=\E[6~, kpp=\E[5~, kspd=^Z,
        nel=^M^J, op=\E[39;49m, rc=\E8, rev=\E[7m, ri=\EM,
        rmacs=\E[10m, rmcup=\E[2J\E[?47l\E8, rmir=\E[4l,
        rmpch=\E[10m, rmso=\E[27m, rmul=\E[24m, rs1=\Ec\E]R,
        sc=\E7, setab=\E[4%p1%dm, setaf=\E[3%p1%dm,
        sgr=\E[0;10%?%p1%t;7%;%?%p2%t;4%;%?%p3%t;7%;%?%p6%t;1%;%?%p7%t;8%;%?%p9%t;11%;m,
        sgr0=\E[0;10m, smacs=\E[11m, smcup=\E7\E[?47h,
        smir=\E[4h, smpch=\E[11m, smso=\E[7m, smul=\E[4m, tsl=\E];,
        u6=\E[%i%d;%dR, u7=\E[6n, u8=\E[?6c, u9=\E[c,
        vpa=\E[%i%p1%dd,

I am able to reproduce this by using git for windows' ssh client to connect to an ubuntu machine.
Comment 8 Marek Safar 2016-04-16 08:32:17 UTC
 .
Comment 9 Anthony Truskinger 2016-10-22 00:57:21 UTC
Hi, I'd like to bump this issue. It has been plaguing me for the better part of a year now and until recently I haven't had the inspiration required to isolate it.

I had already written a bug report before I had found this issue, so I'll just include it below:

Running this program:

```
using System;

namespace ConsoleBug
{
   public class HelloWorld
   {
      public static void Main(string[] args)
      {
         Console.WriteLine("Hello World1");
         Console.WriteLine("Hello World2");
         Console.WriteLine("Hello World3");
         Console.WriteLine("Hello World4");


         Console.WriteLine( Console.ForegroundColor.ToString());
         Console.WriteLine("Hello World5");
         Console.WriteLine("Hello World6");
         Console.WriteLine("Hello World7");
         Console.WriteLine("Hello World8");
         Console.WriteLine("Done");
      }
   }
}
```

Using: 
- Mono JIT compiler version 4.6.1 (Stable 4.6.1.5/ef43c15 Wed Oct 12 09:10:37 UTC 2016)
- Ubuntu 16.04.1 LTS

Compile with: `mcs console_bug.cs`

Expected behaviour:
```
Hello World1
Hello World2
Hello World3
Hello World4
White
Hello World5
Hello World6
Hello World7
Hello World8
Done
```

Actual behaviour:
```
Hello World1
Hello World2
Hello World3
Hello World4
```
Followed by a one-second pause, then the screen clears, and the following is printed:
```
White
Hello World5
Hello World6
Hello World7
Hello World8
Done
```

Diagnosis: This all makes sense when you look at the code and the raw output from `script -q /dev/null -c "mono console_bug.exe" > text.txt; cat -e text.txt`:

```
Hello World1^M$
Hello World2^M$
Hello World3^M$
Hello World4^M$
^[[6n^[[H^[[JWhite^M$
Hello World5^M$
Hello World6^M$
Hello World7^M$
Hello World8^M$
Done^M$
^[[39;49m
```

When the `Console.ForegroundColor` getter is invoked, it has a non-idempotent effect of printing:
- ^[[6n        (query cursor position)
- ^[[H         (set cursor position to top left)
- ^[[J         (clear screen below cursor)

If we follow the code:
- ForeGroundColor getter has non-idempotent effect by calling Init()
  (https://github.com/mono/mono/blob/mono-4.6.1.5/mcs/class/corlib/System/TermInfoDriver.cs#L423)
- Init() calls GetCursorPosition
  (https://github.com/mono/mono/blob/mono-4.6.1.5/mcs/class/corlib/System/TermInfoDriver.cs#L258)
- GetCursorPosition() writes "^[[6n" out and timesout the `ConsoleDriver.InternalKeyAvailable` call after one second
  (https://github.com/mono/mono/blob/mono-4.6.1.5/mcs/class/corlib/System/TermInfoDriver.cs#L450)
- `noGetPosition` is set to true as a result and GetCursor() returns
- Init() for some reason then does a `WriteConsole(clear)` because `noGetPosition` is false - **This is the root cause**
  (https://github.com/mono/mono/blob/mono-4.6.1.5/mcs/class/corlib/System/TermInfoDriver.cs#L263)

This bug is particularly annoying because it happens for me due to a static constructor (hence I can't control it), for a type, in a library that I use, which checks (read only!) the default console foreground color. No other color console functionality is ever used.

IMO the Init() method should not have any side effects - especially when it's called from a getter that is querying the default color. At the very least, can it not clear the screen?
Comment 10 Marek Safar 2016-10-24 09:00:11 UTC
@miguel any chance for you to look into this

Note You need to log in before you can comment on or make changes to this bug.