[glibc stdio bug] stream desynchronization after fork

Maksim Kita kitaetoya@gmail.com
Fri Oct 2 14:20:34 GMT 2020


Hi,

After my investigation of kernel bug related to streams https://bugzilla.kernel.org/show_bug.cgi?id=193381, I found out that the problem is iwth glibc libio stream synchronization between parent and child after fork.

It can be reproduced with any glibc io streams. Problem is with buffering, after we read some data from stream it gets buffered in parent process. Then after fork in child process we read chunk from this buffer. If we strace, we can see that child process performs lseek before exit  to sync buffer.

Strace output in child:

[pid 82662] write(2, "Read in child bcde\n", 19Read in child bcde) = 19
[pid 82662] lseek(3, -3, SEEK_CUR)      = 5
[pid 82662] exit_group(0)               = ?

Glibc stack trace on lseek:

#0  __lseek64 (fd=3, offset=-3, whence=1) at ../sysdeps/unix/sysv/linux/lseek64.c:28\n
#1  0x00007ffff7e575cb in _IO_new_file_sync (fp=0x5555555592a0) at libioP.h:948
#2  0x00007ffff7e5b6a5 in _IO_default_setbuf (fp=fp@entry=0x5555555592a0, p=0x0, len=0) at libioP.h:948
#3  0x00007ffff7e576bd in _IO_new_file_setbuf (fp=0x5555555592a0, p=<optimized out>, len=<optimized out>) at fileops.c:383
#4  0x00007ffff7e5c064 in _IO_unbuffer_all () at libioP.h:948
#5  _IO_cleanup () at genops.c:875
#6  0x00007ffff7e0fb32 in __run_exit_handlers (status=0, listp=<optimized out>, run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true) at exit.c:130
#7  0x00007ffff7e0fbe0 in __GI_exit (status=<optimized out>) at exit.c:139

If we remove buffering from program, issue will not be reproduced.

In attachments I added program to reproduce and test file.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: file_stdio_io.c
Type: text/x-csrc
Size: 1023 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-help/attachments/20201002/1d47005d/attachment.bin>
-------------- next part --------------
abcdefgh


More information about the Libc-help mailing list