Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: C streams are not flushed at program exit #8808

gopherbot opened this issue Sep 25, 2014 · 3 comments

cmd/cgo: C streams are not flushed at program exit #8808

gopherbot opened this issue Sep 25, 2014 · 3 comments


Copy link

@gopherbot gopherbot commented Sep 25, 2014

by kalyanakrishna:

What does 'go version' print?
go version go1.3.1 linux/amd64

What steps reproduce the problem?
Take the first example on this page:


func main() { Example() }
remove "\n" from cs

build and run

What happened?
Nothing prints

What should have happened instead?
it should have printed "Hello from stdio" without a trailing newline

Additional detail:
If "\n" appears in the printf argument anywhere, it will print till \n and
discard everything after \n


if you change cs to "Hello from stdio \n and go ", it will print 

"Hello from stdio"

this will happen even if you modify printf directly

if printf is modified to 

        printf("%s 123 ", s);

it will print till the last "\n" it encounters and discard the rest. so if s
was "hello from stdio\n", it will print "hello from stdio" and
discard 123

If possible, include a link to a program on

import "C" wont work on
Copy link

@ianlancetaylor ianlancetaylor commented Sep 26, 2014

Comment 1:

The problem is that returning from the C main function flushes the C streams, as does
calling the C exit function.  Returning from the Go main function, or calling os.Exit,
does not flush the C streams.  By default C's standard output flushes on newline.  That
is why you see the behaviour that you do.
I'm not sure whether to consider this a bug in Go or not.  You can work around it by
explicitly calling C.fflush(nil) before main returns.

Labels changed: added repo-main, release-none.


Copy link

@gopherbot gopherbot commented Sep 26, 2014

Comment 2 by kalyanakrishna:

if Go is the primary frontend for compiling mixed code, I would categorize it as an
issue in Go. Esp. since Go is separating the codes and compiling them, it might be a
good idea to implicitly mimic C stream flush behavior.
This can be very confusing for new users and a potential headache for large codebases
using mixed languages. Either this needs to be explicitly documented and well publicized
or it should be taken care of implicitly.


Copy link

@dvyukov dvyukov commented Sep 28, 2014

Comment 3:

FWIW I guess we also don't run atexit/atquickexit callbacks.


@bradfitz bradfitz removed the new label Dec 18, 2014
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants