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

Console: attempt to change console text color leads to app deadlock and freeze when user pressing ^C during read from serial port #21773

Open
qrp73 opened this issue May 13, 2024 · 0 comments

Comments

@qrp73
Copy link

qrp73 commented May 13, 2024

Just catch some strange bug with console on linux platform.
If I change Console.ForegroundColor, it leads to app freeze with 100% CPU load when I press ^C during read data from serial port.
When I remove setting of Console.ForegroundColor it works ok with no app freeze.

Also there is some bug with console color, because current Console.ForegroundColor is reported as White, while actual console text color is Gray.

Steps to Reproduce

In order to reproduce it, you're needs to connect some usb-serial adapter and put it's proper name in the code.

  1. Create file test.cs:
using System;
using System.Linq;
using System.IO.Ports;

// Compile and run:
// $ mcs test.cs
// $ mono test.exe

class Program
{
	static void Main(string[] args)
	{
		try
		{
			var foreColor = Console.ForegroundColor;
			Console.WriteLine("foreColor: {0}", foreColor);
			// BUG1: foreColor is White, while actual color is Gray
			
			// BUG2: any attempt to change Console.ForegroundColor
			//       leads to app freeze with 100% CPU load if user 
			//       pressing ^C during SerialPort.Read
			Console.ForegroundColor = ConsoleColor.Yellow;
			Console.WriteLine("Yellow");
			Console.ForegroundColor = foreColor;
			Console.WriteLine("Normal");
			
			Console.WriteLine("Now press Ctrl+C to get app freeze bug");
			
            var serial = new SerialPort();
            serial.PortName = "/dev/ttyUSB0";
            serial.BaudRate = 38400;
            serial.Parity = Parity.None;
            serial.DataBits = 8;
            serial.StopBits = StopBits.One;
            serial.ReadTimeout = 100000;
            serial.Open();
            
			var data = new byte[4];
            var read = serial.Read(data, 0, data.Length);
            Console.WriteLine("Read() = {0}", read);
		}
		catch (Exception ex)
		{
			Console.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message);
		}
	}
}
  1. Compile it with command mcs test.cs
  2. Run it with command mono test.exe

Expected result: it should show correct current console foreground color, which is Gray
Actual result: it shows ```foreColor: White - BUG 1

  1. Press Ctrl+C to interrupt read operation from /dev/ttyUSB0 serial port

Expected result: application is terminated and console returns to user prompt
Actual result: application freeze with 100% CPU load - BUG 2

Current Behavior

  1. Currently Console.ForegroundColor returns incorrect current console color (White instead of Gray).

  2. Any attempt to set Console.ForegroundColor leads to app freeze when user pressing ^C during SerialPort.Read call.

Expected Behavior

  1. Console.ForegroundColor should return correct current console color.

  2. Set Console.ForegroundColor should not lead to app freeze when user pressing ^C during SerialPort.Read call.

On which platforms did you notice this

[ ] Linux

I reproduce it on Raspberry Pi OS Bookworm aarch64 and on Linux Mint on Intel x86-64 platform.

Version Used:

Mono JIT compiler version 6.12.0.200 (tarball Tue Jul 11 21:44:02 UTC 2023)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       normal
	Notifications: epoll
	Architecture:  arm64
	Disabled:      none
	Misc:          softdebug 
	Interpreter:   yes
	LLVM:          yes(610)
	Suspend:       preemptive
	GC:            sgen (concurrent by default)

Stacktrace

Debugger shows that the app freezes at __GI___poll at ./io/../sysdeps/unix/sysv/linux/poll.c:41,-1

20240513_11h59m55s_grim

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

No branches or pull requests

1 participant