[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gambas-bugtracker] Bug #3258: Gambas task bugs, Was having output disapear sometimes when stdout/stderr was a pipe or file


http://gambaswiki.org/bugtracker/edit?object=BUG.3258&from=L21haW4-

Brian G reported a new bug.

Summary
-------

Gambas task bugs, Was having output disapear sometimes when stdout/stderr was a pipe or file

Type             : Bug
Priority         : High
Gambas version   : Master
Product          : Language


Description
-----------

I have a program that print in a background  task, sometime nothing was printed by the task, I use a lot of tasks that talk to each other thru pipes..... yes claude suggested these patches.

--- a/main/gbx/gbx_c_task.c	2026-11-18 17:39:47.762429975 -0800
+++ b/main/gbx/gbx_c_task.c	2026-11-18 17:39:47.893343751 -0800
@@ -265,6 +265,10 @@
 
 static void exit_child(int ret)
 {
+	// _exit() does not flush stdio: when the standard output is a pipe or a file, what the task
+	// printed is still in the buffer and would be lost
+	fflush(stdout);
+	fflush(stderr);
 	FILE_exit();
 	_exit(ret);
 }
@@ -320,6 +324,10 @@
 	sigaddset(&sig, SIGCHLD);
 	sigprocmask(SIG_BLOCK, &sig, &old);
 
+	// The child must not inherit output the parent has not written yet, it would be written twice
+	fflush(stdout);
+	fflush(stderr);
+
 	pid = fork();
 	if (pid == (-1))
 	{

The flush in exit_child() is the fix. The flush before fork() is needed with it: without it, any output the parent had buffered but not written would be copied into the child and printed twice once the child flushes.

For the upstream report: a Task with no Read handler that prints loses its output whenever the program's stdout is a pipe or a file. On a terminal stdout is line-buffered, so nothing is lost.


System information
------------------

[System]
Gambas=3.22.99 8d2fdc7cf (jit-libtcc)
OperatingSystem=Linux
Distribution=Linux Mint 22.3 (Zena)
Kernel=7.2.0-070200-generic
Architecture=x86_64
Cores=8
Memory=13923M
Language=en_US.UTF-8
Platform=x11
Desktop=MATE
DesktopResolution=96
DesktopScale=7
WidgetTheme=proxy
Font=Ubuntu Mono,10
DarkTheme=False
[Programs]
appimage-builder=appimage-builder: 1.1.0
dpkg=Debian 'dpkg' package management program version 1.22.6 (amd64).
gcc=gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
git=git version 2.43.0
msgmerge=msgmerge (GNU gettext-tools) 0.21
rpmbuild=RPM version 4.18.2
tcc=tcc version 0.9.27 (x86_64 Linux)
[Libraries]
Cairo=libcairo.so.2.11800.0
Curl=libcurl.so.4.8.0
DBus=libdbus-1.so.3.32.4
GDK2=libgdk-x11-2.0.so.0.2400.33
GDK3=libgdk-3.so.0.2409.32
GStreamer=libgstreamer-1.0.so.0.2402.0
GTK+2=libgtk-x11-2.0.so.0.2400.33
GTK+3=libgtk-3.so.0.2409.32
OpenGL=libGL.so.1.7.0
Poppler=libpoppler.so.134.0.0
QT5=libQt5Core.so.5.15.13
QT6=libQt6Core.so.6.4.2
RSvg=librsvg-2.so.2.50.0
SDL=libSDL2-2.0.so.0.3000.0
SQLite3=libsqlite3.so.0.8.6
[Environment]
CLUTTER_BACKEND=x11
COMPIZ_CONFIG_PROFILE=mint
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
DESKTOP_SESSION=mate
DISPLAY=:0
EDITOR=kwrite
GB_GUI=gb.qt6
GDMSESSION=mate
GDM_LANG=en_US
GIO_LAUNCHED_DESKTOP_FILE_PID=186672
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
GTK3_MODULES=xapp-gtk3-module
GTK_MODULES=gail:atk-bridge
GTK_OVERLAY_SCROLLING=0
HOME=<home>
LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
LC_ADDRESS=en_US.UTF-8
LC_ALL=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_NUMERIC=en_US.UTF-8
LC_PAPER=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_TIME=en_US.UTF-8
LIBVIRT_DEFAULT_URI=qemu:///system
LOGNAME=<user>
MATE_DESKTOP_SESSION_ID=this-is-deprecated
NVM_BIN=<home>/.nvm/versions/node/v24.11.0/bin
NVM_CD_FLAGS=
NVM_DIR=<home>/.nvm
NVM_INC=<home>/.nvm/versions/node/v24.11.0/include/node
PAPERSIZE=a4
PATH=<home>/.nvm/versions/node/v24.11.0/bin:<home>/.config/emacs/bin:<home>/.local/bin:<home>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin
PICO_SDK_PATH=<home>/Projects/picomite/pico/pico-sdk
PWD=<home>
QT_ACCESSIBILITY=1
QT_FONT_DPI=96
QT_LOGGING_RULES=*.debug=false
QT_SCALE_FACTOR=1
SESSION_MANAGER=local/<hostname>:@/tmp/.ICE-unix/4134,unix/<hostname>:/tmp/.ICE-unix/4134
SHELL=/usr/bin/gsh
SHLVL=0
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
TZ=:/etc/localtime
USER=<user>
XAUTHORITY=<home>/.Xauthority
XDG_ACTIVATION_TOKEN=mate-panel-4661-<hostname>-gambas3-123_TIME770807223
XDG_CONFIG_DIRS=/etc/xdg/xdg-mate:/etc/xdg
XDG_CURRENT_DESKTOP=MATE
XDG_DATA_DIRS=/usr/share/mate:<home>/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/<user>
XDG_RUNTIME_DIR=/run/user/1000
XDG_SEAT=seat0
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
XDG_SESSION_CLASS=user
XDG_SESSION_DESKTOP=mate
XDG_SESSION_ID=c2
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
XDG_SESSION_TYPE=x11
XDG_VTNR=7
gshlibrary=<home>/Projects/gshlibrary
gshproject=<home>/Projects/gsh



----[ Gambas bugtracker-list is hosted by https://www.hostsharing.net ]----