Skip to content

Output lost for process with detached terminal (sometimes!) #11254

@throwable-one

Description

@throwable-one

Windows Version

Microsoft Windows [Version 10.0.19045.4046]

WSL Version

2.1.4.0

Are you using WSL 1 or WSL 2?

  • WSL 2
  • WSL 1

Kernel Version

5.15.146.1-2

Distro Version

Debian 12

Other Software

No response

Repro Steps

Compile and execute the following program that uses daemon(3) to detach from controlled terminal.

#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>


int main() {
        if (daemon(0, 1) != 0) { // detach from terminal but leave stdout untouched!
                fprintf(stderr, "daemon error %d\n", errno);
                return 1;
        }

        int fd = open(ctermid(NULL), O_RDWR);
        if (fd == -1) {
                printf("I have no terminal\n");
                return 0;
        } else {
                fprintf(stderr, "I HAVE terminal, failed to daemonize??\n");
                close(fd);
                return 1;
        }

}

Compile it and run several times to make sure it works

$ cc 1.c
$ ./a.out
I have no terminal
$ ./a.out
I have no terminal
$ ./a.out
I have no terminal
$ ./a.out
$ I have no terminal

Everything works as expected.

Now, run it via wsl.exe

c:\>wsl.exe --exec "/home/link/a.out"

c:\>wsl.exe --exec "/home/link/a.out"
I have no terminal

c:\>wsl.exe --exec "/home/link/a.out"

c:\>wsl.exe --exec "/home/link/a.out"

Sometimes it work, sometimes doesn't. I assume that stdout is broken when process doesn't have a terminal.
This is probably the reason fake terminal with funny size exists for redirected processes: #10701

Expected Behavior

"I have no terminal" string printed

Actual Behavior

Sometimes it does, sometimes it doesn't

Diagnostic Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions