Issue 17797 - [REG 2.073.2] double print to stdout when reading stdin in another thread
Summary: [REG 2.073.2] double print to stdout when reading stdin in another thread
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P1 regression
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-31 13:40 UTC by Ivan Kazmenko
Modified: 2024-12-01 16:30 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Ivan Kazmenko 2017-08-31 13:40:19 UTC
Consider the following program:

-----
import std.stdio, core.thread;

void main(){	
	auto thread = new Thread(&func).start;
        writeln("Output");
	writeln("Output2");
	writeln("Output3");
	while(true){}
}

void func(){
    foreach(line; stdin.byLineCopy){}
}
-----

The correct output is:
-----
Output
Output2
Output3
-----

This starts a thread to read from stdin, and then writes several lines to stdout in the main thread and enters and infinite loop.

When compiled with dmd 2.073.1 or earlier, everything is fine.
Starting from dmd 2.073.2, the output is unstable but usually not correct.
Locally, I usually observe the following output:

-----
Output
Output2
Output3
Output2
-----

Or sometimes:

-----
Output
Output2
Output2
Output3
-----

Here are the steps to reproduce, tested on 64-bit Windows with a 32-bit compiler.

1. Use dmd 2.073.2 or later.
2. Compile like "dmd program.d" (compiler switches don't seem to matter).
3. Run a command prompt, like "cmd.exe".
4. Execute "program.exe" (importantly, without any I/O redirection).

Original thread at D.Learn:
https://forum.dlang.org/post/ozbhjgvqmurphkeosrpf@forum.dlang.org
Comment 1 Walter Bright 2017-10-03 08:39:15 UTC
It happens on Win32 as well, which uses the DMC runtime library, not Microsoft's. So it is not specific to the Microsoft runtime library.

I suspect it is a Phobos problem, not a compiler issue, and changed the category accordingly.
Comment 2 ag0aep6g 2017-10-03 09:16:11 UTC
(In reply to Walter Bright from comment #1)
> It happens on Win32 as well, which uses the DMC runtime library, not
> Microsoft's. So it is not specific to the Microsoft runtime library.

It *only* happens with the DMC runtime. People are running 64-bit Windows, but they're not using 64-bit dmd. I just tested in a virtual machine with -m32mscoff and didn't see duplicated lines.
Comment 3 Martin Nowak 2017-10-09 11:26:58 UTC
cat > bug.d << CODE
import std.stdio, core.thread;

void main()
{
    auto thread = new Thread(&func).start;
    writeln("Output");
    writeln("Output2");
    writeln("Output3");
    thread.join();
}

void func()
{
    foreach (line; stdin.byLineCopy)
    {
    }
}
CODE
dmd -run bug.d
----
Slightly nicer version that terminates on EOF (Ctrl+D), works as expected on linux.

Sure about 2.073.2? The only interesting change in

https://github.com/dlang/dmd/compare/v2.073.1...v2.073.2
https://github.com/dlang/druntime/compare/v2.073.1...v2.073.2
https://github.com/dlang/phobos/compare/v2.073.1...v2.073.2

is https://github.com/dlang/dmd/pull/6582.
Comment 4 Rainer Schuetze 2017-10-10 07:03:15 UTC
I could verify it happens with 2.073.2, but not 2.073.1. This is caused by an updated snn.lib. Might be introduced by the fix for issue 13727.
Comment 5 dlangBugzillaToGithub 2024-12-01 16:30:44 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10262

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB