classification
Title: print() function hangs on MS-Windows 10
Type: Stage: resolved
Components: Interpreter Core, Windows Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, jkloth, malin, paul.moore, steve.dower, terry.reedy, tim.golden, vstinner, zach.ware
Priority: normal Keywords:

Created on 2016-04-13 01:57 by malin, last changed 2017-03-09 18:36 by eryksun. This issue is now closed.

Messages (11)
msg263295 - (view) Author: Ma Lin (malin) * Date: 2016-04-13 01:57
My OS is MS-Windows 10 X86-64 (Home edition), with the lastest update (now it's 10586.164).

I have two programs, they occasionally infinite hang.
After a few months observation, I provide these infomation:

1, print() function cause the infinite hang.
2, If it hangs, simply press ENTER key, it goes on without any problem.
3, Both pure-console program and console in Tkinter program have this issue.
4, I tried offical Python 3.4.4 64bit and 3.5.1 64bit, they all have this issue. I didn't try other versions because my programs need Python 3.4+.
5, IIRC, the problem was since Windows 10 Threshold 2 (12/Nov/2015), but I'm not very sure about this.
msg263316 - (view) Author: Ma Lin (malin) * Date: 2016-04-13 09:29
After my MS-Windows 10 updated to 10586.218 this day, it occurs again.

It hung, the console cursor was flashing at the beginning of the current line.
I pressed ENTER key on the console window, then it printed the supposed content and went on.

I have read issue26624, if you encounter the issue again, try ENTER key, if it works maybe it's the same problem.
msg263317 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-04-13 09:32
> I have two programs, they occasionally infinite hang.

Can you please try the faulthandler module to try to get a traceback of all Python threads?

I suggest to use the watchdog with a short timeout (ex: 60 seconds):
https://docs.python.org/dev/library/faulthandler.html#faulthandler.dump_traceback_later

> 1, print() function cause the infinite hang.
> 2, If it hangs, simply press ENTER key, it goes on without any problem.

Are you running your program in the Windows console (cmd.exe)? In IDLE? In PowerShell?
msg263318 - (view) Author: Ma Lin (malin) * Date: 2016-04-13 09:48
ok, I will try faulthandler module.

>> Are you running your program in the Windows console (cmd.exe)? In IDLE? In PowerShell?

One runs in pure command line mode, cmd.exe.
The other is a simple tkinter GUI with a console for output message.

To be honest, I'm a bit suprise that almost no one complain it, maybe it's my system's fault? I don't know.

Just want to remind you try ENTER key next time.
msg263319 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-04-13 09:49
> To be honest, I'm a bit suprise that almost no one complain it, maybe it's my system's fault? I don't know.

It looks like a bug in your application. Can you provide a short script to reproduce the bug?
msg263320 - (view) Author: Ma Lin (malin) * Date: 2016-04-13 10:12
>> It looks like a bug in your application. Can you provide a short script to reproduce the bug?

It's hard to believe it, the code has no way to behavior like that.
I think this is not reproducible, it's random hang.

As my subjective feelings, I suspect it's a Windows bug.

e.g., after an relative long time nooperation, I suddenly press a button in tkinter GUI, it simply delete a file like this:

        try:
            output = self.output.get().strip()
            os.remove(output)
        except:
            pass
        else:
            print('已删除输出文件')
			
It usually hangs at the print() line.

Another scene is high IO, I download something at 8 MB/s, then I run my program, it should download a HTML page and then print() some information, some hangs happed in this scene.

In addition, the printed strings are Simplified Chinese, my system's language is it as well. Maybe it's a factor?

I'm inclined to guess is a Windows 10 (or only my system's) bug.
msg263323 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-04-13 11:04
> In addition, the printed strings are Simplified Chinese, my system's language is it as well. Maybe it's a factor?

Ooooooh yes. The Windows Console sucks to display Unicode: see the very old issue #1602 which is not fixed yet and is 9 years old...

Try to replace your chinese message with a simple ASCII text like print("test") to check if it works around your issue.

Maybe IDLE and/or PowerShell have a better Unicode support, I don't know for Chinese.

Sorry, I know better Linux, I know that Linux handles well Unicode in terminals.
msg263324 - (view) Author: Ma Lin (malin) * Date: 2016-04-13 11:17
I'm afraid not. If run it 1000 times, only about <5 times occurs, the others are quite fine.
I have installed faulthandler, let's wait the hunting.
msg263602 - (view) Author: Ma Lin (malin) * Date: 2016-04-17 10:02
faulthandler module caught a hang just now:

    Timeout (0:01:00)!
    Thread 0x00000eb0 (most recent call first):
      File "D:\git\tz2txt\tz2txt\gui.py", line 262 in delfile
      File "C:\Python35\lib\tkinter\__init__.py", line 1549 in __call__
      File "C:\Python35\lib\tkinter\__init__.py", line 1131 in mainloop
      File "D:\git\tz2txt\tz2txt\gui.py", line 328 in main
      File "D:\git\tz2txt\tz2txt\gui.py", line 331 in <module>
  
 Here is the full delfile function:
  
      def delfile(self):
        try:
            output = self.output.get().strip()
            os.remove(output)
        except:
            pass
        else:
            print('已删除输出文件')    # <- line 262
        
        try:
            os.remove(discard_fn)
        except:
            pass
        else:
            print('已删除丢弃文件')
        
        self.url.set(url_use)

~~~~~~~~~~~~~~~~~~~~~~~~~~
Some supplemental information:

There was a time point, *maybe* it's Windows10 Threshold2 (12/Nov/2015), before that time no hang at all.
After that time, the hangs were quite often.
Some months later, the frequency had declined a lot, but still hang occasionally.
msg289289 - (view) Author: Ma Lin (malin) * Date: 2017-03-09 13:05
This is an invalid issue, very sorry for waste your time! Especially apologize to Stinner.

After enabling `QuickEdit Mode`, then click the console will suspend the program.
msg289321 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-03-09 18:36
> After enabling `QuickEdit Mode`, then click the console will 
> suspend the program.

That's not suspending the entire process. It's blocking the thread that writes to the console. The user-mode WriteConsole function is implemented by a synchronous NtDeviceIoControlFile system call. The console (i.e. conhost.exe) is on the other end of the call. It has at least 2 threads -- an input thread that's basically a standard message loop and an output thread for updating and rendering the contents of its active screen buffer. It's a feature that clicking on the console window to copy text causes its output thread to block. This in turn prevents WriteFile in python.exe from completing.

That said, I've seen random hangs in the console that are unrelated to the above (e.g. hanging while reflowing text), but not often enough that I bothered to investigate it. There's a lot of new code in the 2nd generation console implementation (ConhostV2.dll) in Windows 10, and the developers are making significant improvements with each update. I'm waiting for the dust to settle.
History
Date User Action Args
2017-03-09 18:36:39eryksunsetmessages: + msg289321
2017-03-09 13:05:04malinsetstatus: open -> closed
resolution: not a bug
messages: + msg289289

stage: resolved
2016-04-17 10:02:29malinsetmessages: + msg263602
2016-04-15 19:46:05terry.reedysetnosy: + terry.reedy
2016-04-13 11:17:15malinsetmessages: + msg263324
2016-04-13 11:04:36vstinnersetmessages: + msg263323
2016-04-13 10:12:52malinsetmessages: + msg263320
2016-04-13 09:49:20vstinnersetmessages: + msg263319
2016-04-13 09:48:21malinsetmessages: + msg263318
2016-04-13 09:32:13vstinnersetmessages: + msg263317
2016-04-13 09:29:10malinsetnosy: + vstinner, jkloth, eryksun
messages: + msg263316
components: + Interpreter Core
2016-04-13 01:57:49malincreate