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

x/playground: stdout and stderr not interleaved with defer #10279

Closed
clausecker opened this issue Mar 28, 2015 · 8 comments
Closed

x/playground: stdout and stderr not interleaved with defer #10279

clausecker opened this issue Mar 28, 2015 · 8 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@clausecker
Copy link

Consider the following program:

package main

import "fmt"

var z = 1

func main() {

    defer increaseZ(10)
    defer fmt.Println("z =", increaseZ(20), "Deferred Value 1")
     defer fmt.Println("z =", increaseZ(30), "Deferred Value 2")

   fmt.Println("z =", z, "Main Value")
}

func increaseZ(y int) int {
    z += y
    println("z =", z, "Inside Increase Function")
    return z
}

On my machine (amd64 Linux), this program prints as expected:

z = 21 Inside Increase Function
z = 51 Inside Increase Function
z = 51 Main Value
z = 51 Deferred Value 2
z = 21 Deferred Value 1
z = 61 Inside Increase Function

whereas on Playground, it prints the following incorrect output:

z = 21 Inside Increase Function
z = 51 Inside Increase Function
z = 61 Inside Increase Function
z = 51 Main Value
z = 51 Deferred Value 2
z = 21 Deferred Value 1

I have no idea what causes this effect but it seems that the first deferred function call is executed before the final fmt.Println call in main.

@bradfitz
Copy link
Contributor

Are you sure this isn't just the result of mixing stderr and stdout differently?

Try it using only one print function.

@clausecker
Copy link
Author

Yes, indeed. I'm closing this now. Please mark as “invalid.”

@clausecker
Copy link
Author

It's still weird that I cannot reproduce this behaviour without deference. Go seems to obey the order in which I output things in other cases.

@bradfitz bradfitz changed the title Deference doesn't work correctly in playground. playground: stdout and stderr not interleaved with defer Mar 29, 2015
@bradfitz
Copy link
Contributor

Reopening with new title.

I think the playground's hooks for intercepting os.Stdout and os.Stderr don't work for the builtin println function, because that writes to stderr directly, without going through the playground's hooks.

These work, using only one style of printing:

http://play.golang.org/p/c0XFpaPnh8 -- with println (stderr)
http://play.golang.org/p/M0y2nixfak -- with fmt.Println (stdout)

This is wrong, or at least confusing:

http://play.golang.org/p/L0ys_wO462

/cc @adg (who's on vacation for ~5 more weeks)

@bradfitz bradfitz reopened this Mar 29, 2015
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title playground: stdout and stderr not interleaved with defer x/playground: stdout and stderr not interleaved with defer Apr 14, 2015
@rsc rsc modified the milestones: Unreleased, Unplanned Apr 14, 2015
@ysmolski
Copy link
Member

ysmolski commented Mar 13, 2018

package main

import "fmt"

func main() {
	defer println("D")
	defer fmt.Println("C")
	println("A")
	fmt.Println("B")
}

This runs correctly on playground: http://play.golang.org/p/L0ys_wO462

A
B
C
D

Same is valid for the program on the top.

/CC @bradfitz

@ysmolski
Copy link
Member

After running couple of more attempts to break on playground, I can say that this works as designed. We can close it.

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 13, 2018
@andybons
Copy link
Member

Closing as
http://play.golang.org/p/c0XFpaPnh8 -- with println (stderr)
http://play.golang.org/p/M0y2nixfak -- with fmt.Println (stdout)

and the mixed version http://play.golang.org/p/L0ys_wO462

all have the same output of

A
B
C
D

@ysmolski
Copy link
Member

Thanks @andybons !

@golang golang locked and limited conversation to collaborators Mar 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

7 participants