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

-prod affects WaitingGroup #2874

Closed
radare opened this issue Nov 24, 2019 · 7 comments
Closed

-prod affects WaitingGroup #2874

radare opened this issue Nov 24, 2019 · 7 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@radare
Copy link
Contributor

radare commented Nov 24, 2019

V version: V 0.1.22 19b4cf6
OS: macOS 10.15

What did you do?

$ cat match.v
import sync
import time

fn sub(wg mut sync.WaitGroup) {
	time.sleep(1)
	wg.done()
}

fn main() {
	mut wg := sync.new_waitgroup()
	wg.add(3)
	go sub(&wg)
	go sub(&wg)
	go sub(&wg)
	wg.wait()
}

What did you expect to see?

$ v -prod match.v
$ time ./match
./match  0.99s user 0.01s system 99% cpu 1.014 total
$

What did you see instead?

when using -prod i get undefined behaviour (sometimes ends quickly, sometimes never ends) related to WaitingGroup. it seems to be more consistent without release builds

$ time ./match
^C
./match  0.00s user 0.00s system 0% cpu 14.333 total
$
$ v -prod match.v
$ time ./match
./match  0.00s user 0.00s system 56% cpu 0.005 total
$ time ./match
./match  0.00s user 0.00s system 0% cpu 8.208 total
$ v match.v
$ time ./match
./match  1.00s user 0.00s system 73% cpu 1.369 total
$
@radare radare added the Bug This tag is applied to issues which reports bugs. label Nov 24, 2019
@spytheman
Copy link
Member

It happens on linux too.

@spytheman
Copy link
Member

Passing -O1 (or -O2 or -O3) to the C backend induces this bug. Not sure what the cause is yet.

@radare
Copy link
Contributor Author

radare commented Nov 25, 2019

Found the bug.. the compiler is optimizing this empty loop. imho V should forbid empty loops.

pub fn (wg mut WaitGroup) wait() {
println('wait0')
        for wg.active > 0 {
println('wait1')
                // time.sleep(1)
                // waiting
        }
println('wait2')
}

if you uncomment this time.sleep(1) line then the bug appears actually you can add any other statement inside the for loop and the bug disapears.

@radare
Copy link
Contributor Author

radare commented Nov 25, 2019

not to mention this infinite loop should be using a mutex

@dumblob
Copy link
Contributor

dumblob commented Nov 27, 2019

@medvednikov wanted to completely reimplement go routines (see e.g. #1868 ), so I wouldn't expect the current implementation to behave nicely (but just correctly).

@medvednikov
Copy link
Member

Fixed.

@radare
Copy link
Contributor Author

radare commented Dec 3, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

4 participants