`Hi everybody,
I'm playing around with Goroutine and got a strange issue.
I tried to run the following code:
package main
import (
"fmt"
"time"
)
func loop(c chan int) {
i := 0
for {
time.Sleep(time.Second / 30)
i = i + 1
c <- i
}
}
func main() {
num := make(chan int)
go loop(num)
for {
select {
case count := <-num:
fmt.Printf("Current: %d\n", count)
}
}
}
It's just a simple goroutine that execute 30 times per second, and it should run forever.
This code run normally on Mac OS X 10.11 and Arch Linux Kernel 4.6.3
But when I run it on macOS 10.12 beta 2, the loop stop updating after running for a while.
Go version is 1.6.2 for all environment.
Here is the screen cap:
https://www.youtube.com/watch?v=lre18XwZMOo
I also tried to stop it with:
And this is the output I got, it seems to be stopped at time.Sleep statement:
SIGABRT: abort
PC=0x56c7b m=0
goroutine 0 [idle]:
runtime.mach_semaphore_wait(0xc800000107, 0xc800000000, 0x1, 0x10000000002c401, 0xc820000180, 0x1835c0, 0x4b2a9, 0xffffffffffffffff, 0x100000000, 0x7fff5fbff8ec, ...)
/usr/local/go/src/runtime/sys_darwin_amd64.s:411 +0xb
runtime.semasleep1(0xffffffffffffffff, 0x100000000)
/usr/local/go/src/runtime/os1_darwin.go:423 +0xdf
runtime.semasleep.func1()
/usr/local/go/src/runtime/os1_darwin.go:439 +0x29
runtime.systemstack(0x7fff5fbff8f0)
/usr/local/go/src/runtime/asm_amd64.s:307 +0xab
runtime.semasleep(0xffffffffffffffff, 0x0)
/usr/local/go/src/runtime/os1_darwin.go:440 +0x36
runtime.notesleep(0x183a08)
/usr/local/go/src/runtime/lock_sema.go:166 +0xed
runtime.stopm()
/usr/local/go/src/runtime/proc.go:1538 +0x10b
runtime.findrunnable(0xc820018000, 0x0)
/usr/local/go/src/runtime/proc.go:1976 +0x739
runtime.schedule()
/usr/local/go/src/runtime/proc.go:2075 +0x24f
runtime.park_m(0xc820000180)
/usr/local/go/src/runtime/proc.go:2140 +0x18b
runtime.mcall(0x7fff5fbffa70)
/usr/local/go/src/runtime/asm_amd64.s:233 +0x5b
goroutine 1 [chan receive]:
main.main()
/Users/huy/code/go/sleep/main.go:22 +0x99
goroutine 5 [sleep]:
time.Sleep(0x1fca055)
/usr/local/go/src/runtime/time.go:59 +0xf9
main.loop(0xc82000e0c0)
/Users/huy/code/go/sleep/main.go:11 +0x29
created by main.main
/Users/huy/code/go/sleep/main.go:19 +0x67
rax 0xe
rbx 0x183900
rcx 0x7fff5fbff878
rdx 0x7fff5fbff8f0
rdi 0x107
rsi 0x1835c0
rbp 0x107
rsp 0x7fff5fbff878
r8 0x183900
r9 0xc82001aa00
r10 0xc820019ac0
r11 0x286
r12 0x0
r13 0x27
r14 0x10
r15 0x10f300
rip 0x56c7b
rflags 0x286
cs 0x7
fs 0x0
gs 0x0
Does anybody know why this happen? Is this the issue of the compiler?
Thanks,
Huy