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

Open
gopherbot opened this issue Sep 25, 2014 · 3 comments
Open

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

gopherbot opened this issue Sep 25, 2014 · 3 comments
Milestone

Comments

@gopherbot
Copy link

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:
https://code.google.com/p/go-wiki/wiki/cgo

Add 

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

ex:

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

ex:
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 play.golang.org.

import "C" wont work on play.golang.org
@ianlancetaylor
Copy link
Contributor

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.

@gopherbot
Copy link
Author

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.

@dvyukov
Copy link
Member

dvyukov commented Sep 28, 2014

Comment 3:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants