go: runtime: "fatal: morestack on g0" on amd64 after upgrade to Go 1.21

Does this issue reproduce with the latest release?

Yes.

What did you do?

https://github.com/VirrageS/fluent-bit-go-bug here is full code to repo. I will try to make it smaller but for now this is all I have.

When running this project with:

docker build --build-arg="GO_VERSION=1.20" -t fluentbit .
docker run -t fluentbit

it runs correctly.

But when I change to GO_VERSION=1.21 it fails with fatal: morestack on g0:

docker build --build-arg="GO_VERSION=1.21" -t fluentbit .
docker run -t fluentbit

It looks like regression in Go 1.21. I found similar issues but those seem to be quite old and are not tied to Go 1.21 version.

I have tried to strace the binary and this is what I got:

Details

18    <... set_robust_list resumed>)    = 0
18    prctl(PR_SET_NAME, "flb-pipeline" <unfinished ...>
18    <... prctl resumed>)              = 0
18    futex(0x55c46520b460, FUTEX_WAKE_PRIVATE, 2147483647 <unfinished ...>
18    <... futex resumed>)              = 0
18    mmap(NULL, 2097152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x7f93877fd000
18    munmap(0x7f93877fd000, 2097152 <unfinished ...>
18    <... munmap resumed>)             = 0
18    mmap(NULL, 4190208, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x7f93875fe000
18    munmap(0x7f93875fe000, 8192 <unfinished ...>
18    <... munmap resumed>)             = 0
18    munmap(0x7f9387800000, 2084864 <unfinished ...>
18    <... munmap resumed>)             = 0
18    mmap(NULL, 2097152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0 <unfinished ...>
18    <... mmap resumed>)               = 0x7f9387400000
18    epoll_create1(EPOLL_CLOEXEC)      = 8
18    pipe([9, 10])                     = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 9, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=3559108056, u64=140272896028120}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    futex(0x55c46520b480, FUTEX_WAKE_PRIVATE, 2147483647) = 0
18    epoll_create1(EPOLL_CLOEXEC <unfinished ...>
18    <... epoll_create1 resumed>)      = 11
18    pipe([12, 13])                    = 0
18    epoll_ctl(11, EPOLL_CTL_ADD, 12, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269204480, u64=140271606124544}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    pipe([14, 15])                    = 0
18    epoll_ctl(11, EPOLL_CTL_ADD, 14, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269286400, u64=140271606206464}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    pipe([16, 17])                    = 0
18    epoll_ctl(11, EPOLL_CTL_ADD, 16, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269286592, u64=140271606206656}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f9386bff000
18    mprotect(0x7f9386c00000, 8388608, PROT_READ|PROT_WRITE|PROT_EXEC <unfinished ...>
18    <... mprotect resumed>)           = 0
18    clone(child_stack=0x7f93873f54f0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID <unfinished ...>
18    <... clone resumed>, parent_tid=[19], tls=0x7f93873ff700, child_tidptr=0x7f93873ff9d0) = 19
18    futex(0x7f93874150a8, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
18    <... futex resumed>)              = 0
18    futex(0x7f93874150b0, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
18    <... futex resumed>)              = 0
18    getpid()                          = 10
18    openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC <unfinished ...>
18    <... openat resumed>)             = 18
18    fstat(18,  <unfinished ...>
18    <... fstat resumed>{st_mode=S_IFREG|0644, st_size=118, ...}) = 0
18    fstat(18,  <unfinished ...>
18    <... fstat resumed>{st_mode=S_IFREG|0644, st_size=118, ...}) = 0
18    read(18, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1\0\0\0\1\0\0\0\0"..., 4096) = 118
18    lseek(18, -62, SEEK_CUR <unfinished ...>
18    <... lseek resumed>)              = 56
18    read(18, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1\0\0\0\1\0\0\0\0"..., 4096) = 62
18    close(18 <unfinished ...>
18    <... close resumed>)              = 0
18    write(15, "T\0\0\0\0\0\0\0[2023/09/04 10:18:13] [ "..., 4096 <unfinished ...>
18    <... write resumed>)              = 4096
18    pipe( <unfinished ...>
18    <... pipe resumed>[18, 19])       = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 18, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=3559107648, u64=140272896027712}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    write(15, "m\0\0\0\0\0\0\0[2023/09/04 10:18:13] [ "..., 4096 <unfinished ...>
18    <... write resumed>)              = 4096
18    write(15, "7\0\0\0\0\0\0\0[2023/09/04 10:18:13] [ "..., 4096 <unfinished ...>
18    <... write resumed>)              = 4096
18    write(15, "7\0\0\0\0\0\0\0[2023/09/04 10:18:13] [ "..., 4096 <unfinished ...>
18    <... write resumed>)              = 4096
18    timerfd_create(CLOCK_MONOTONIC, 0 <unfinished ...>
18    <... timerfd_create resumed>)     = 20
18    timerfd_settime(20, TFD_TIMER_ABSTIME, {it_interval={tv_sec=10, tv_nsec=0}, it_value={tv_sec=16839370, tv_nsec=0}},  <unfinished ...>
18    <... timerfd_settime resumed>NULL) = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 20, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269405184, u64=140271606325248}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    write(15, "=\0\0\0\0\0\0\0[2023/09/04 10:18:13] [ "..., 4096) = 4096
18    write(15, "X\0\0\0\0\0\0\0[2023/09/04 10:18:13] [ "..., 4096 <unfinished ...>
18    <... write resumed>)              = 4096
18    pipe( <unfinished ...>
18    <... pipe resumed>[21, 22])       = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 21, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=3559552000, u64=140272896472064}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    openat(AT_FDCWD, "/sys/devices/system/cpu/online", O_RDONLY|O_CLOEXEC <unfinished ...>
18    <... openat resumed>)             = 23
18    read(23,  <unfinished ...>
18    <... read resumed>"0-11\n", 8192) = 5
18    close(23)                         = 0
18    openat(AT_FDCWD, "/proc/stat", O_RDONLY <unfinished ...>
18    <... openat resumed>)             = 23
18    fstat(23, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
18    read(23,  <unfinished ...>
18    <... read resumed>"cpu  531693645 1691808 251625914"..., 1024) = 1024
18    close(23)                         = 0
18    pipe( <unfinished ...>
18    <... pipe resumed>[23, 24])       = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 23, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=3559483392, u64=140272896403456}}) = 0
18    futex(0x55c46520b45c, FUTEX_WAKE_PRIVATE, 2147483647 <unfinished ...>
18    <... futex resumed>)              = 0
18    epoll_create1(EPOLL_CLOEXEC)      = 25
18    pipe( <unfinished ...>
18    <... pipe resumed>[26, 27])       = 0
18    epoll_ctl(25, EPOLL_CTL_ADD, 26, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269552640, u64=140271606472704}}) = 0
18    pipe( <unfinished ...>
18    <... pipe resumed>[28, 29])       = 0
18    epoll_ctl(11, EPOLL_CTL_ADD, 28, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269286784, u64=140271606206848}}) = 0
18    mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f93863fe000
18    mprotect(0x7f93863ff000, 8388608, PROT_READ|PROT_WRITE|PROT_EXEC <unfinished ...>
18    <... mprotect resumed>)           = 0
18    clone(child_stack=0x7f9386bf44f0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID <unfinished ...>
18    <... clone resumed>, parent_tid=[20], tls=0x7f9386bfe700, child_tidptr=0x7f9386bfe9d0) = 20
18    pipe( <unfinished ...>
18    <... pipe resumed>[30, 31])       = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 30, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=3559484160, u64=140272896404224}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    rt_sigprocmask(SIG_SETMASK, NULL,  <unfinished ...>
18    <... rt_sigprocmask resumed>[], 8) = 0
18    rt_sigprocmask(SIG_SETMASK, ~[],  <unfinished ...>
18    <... rt_sigprocmask resumed>NULL, 8) = 0
18    sched_getaffinity(18, 32,  <unfinished ...>
18    <... sched_getaffinity resumed>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) = 8
18    sigaltstack(NULL, {ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}) = 0
18    sigaltstack({ss_sp=0xc000180000, ss_flags=0, ss_size=32768},  <unfinished ...>
18    <... sigaltstack resumed>NULL)    = 0
18    rt_sigprocmask(SIG_SETMASK, [],  <unfinished ...>
18    <... rt_sigprocmask resumed>NULL, 8) = 0
18    gettid( <unfinished ...>
18    <... gettid resumed>)             = 18
18    futex(0x7f93d3dcc900, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
18    <... futex resumed>)              = 1
18    futex(0x7f93d3dcc148, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
18    <... futex resumed>)              = 1
18    futex(0xc000100d48, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
18    <... futex resumed>)              = -1 EAGAIN (Resource temporarily unavailable)
18    --- SIGURG {si_signo=SIGURG, si_code=SI_TKILL, si_pid=10, si_uid=0} ---
18    rt_sigreturn({mask=[]} <unfinished ...>
18    <... rt_sigreturn resumed>)       = -1 EAGAIN (Resource temporarily unavailable)
18    write(1, "[flb-go] plugin parameter = ''\n", 31 <unfinished ...>
18    <... write resumed>)              = 31
18    timerfd_create(CLOCK_MONOTONIC, 0 <unfinished ...>
18    <... timerfd_create resumed>)     = 36
18    timerfd_settime(36, TFD_TIMER_ABSTIME, {it_interval={tv_sec=1, tv_nsec=0}, it_value={tv_sec=16839361, tv_nsec=0}},  <unfinished ...>
18    <... timerfd_settime resumed>NULL) = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 36, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=3559107912, u64=140272896027976}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    timerfd_create(CLOCK_MONOTONIC, 0 <unfinished ...>
18    <... timerfd_create resumed>)     = 37
18    timerfd_settime(37, TFD_TIMER_ABSTIME, {it_interval={tv_sec=5, tv_nsec=0}, it_value={tv_sec=16839365, tv_nsec=0}},  <unfinished ...>
18    <... timerfd_settime resumed>NULL) = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 37, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269406144, u64=140271606326208}}) = 0
18    timerfd_create(CLOCK_MONOTONIC, 0 <unfinished ...>
18    <... timerfd_create resumed>)     = 38
18    timerfd_settime(38, TFD_TIMER_ABSTIME, {it_interval={tv_sec=1, tv_nsec=0}, it_value={tv_sec=16839361, tv_nsec=0}},  <unfinished ...>
18    <... timerfd_settime resumed>NULL) = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 38, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269396208, u64=140271606316272}}) = 0
18    write(15, "<\0\0\0\0\0\0\0[2023/09/04 10:18:13] [ "..., 4096 <unfinished ...>
18    <... write resumed>)              = 4096
18    timerfd_create(CLOCK_MONOTONIC, 0 <unfinished ...>
18    <... timerfd_create resumed>)     = 39
18    timerfd_settime(39, TFD_TIMER_ABSTIME, {it_interval={tv_sec=1, tv_nsec=0}, it_value={tv_sec=16839361, tv_nsec=0}},  <unfinished ...>
18    <... timerfd_settime resumed>NULL) = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 39, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269405504, u64=140271606325568}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    timerfd_create(CLOCK_MONOTONIC, 0) = 40
18    timerfd_settime(40, TFD_TIMER_ABSTIME, {it_interval={tv_sec=1, tv_nsec=500000000}, it_value={tv_sec=16839361, tv_nsec=0}},  <unfinished ...>
18    <... timerfd_settime resumed>NULL) = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 40, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269406304, u64=140271606326368}}) = 0
18    timerfd_create(CLOCK_MONOTONIC, 0 <unfinished ...>
18    <... timerfd_create resumed>)     = 41
18    timerfd_settime(41, TFD_TIMER_ABSTIME, {it_interval={tv_sec=0, tv_nsec=250000000}, it_value={tv_sec=16839360, tv_nsec=0}}, NULL) = 0
18    epoll_ctl(8, EPOLL_CTL_ADD, 41, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=2269406464, u64=140271606326528}} <unfinished ...>
18    <... epoll_ctl resumed>)          = 0
18    write(7, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8,  <unfinished ...>
18    <... epoll_wait resumed>[{EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, -1) = 1
18    read(41, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8,  <unfinished ...>
18    <... epoll_wait resumed>[], 256, 0) = 0
18    epoll_wait(8,  <unfinished ...>
18    <... epoll_wait resumed>[{EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, -1) = 1
18    read(41, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [], 256, 0)         = 0
18    epoll_wait(8, [{EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, -1) = 1
18    read(41, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [], 256, 0)         = 0
18    epoll_wait(8, [{EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, -1) = 1
18    read(41, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [], 256, 0)         = 0
18    epoll_wait(8,  <unfinished ...>
18    <... epoll_wait resumed>[{EPOLLIN, {u32=2269396208, u64=140271606316272}}, {EPOLLIN, {u32=2269405504, u64=140271606325568}}, {EPOLLIN, {u32=2269406304, u64=140271606326368}}, {EPOLLIN, {u32=3559107912, u64=140272896027976}}, {EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, -1) = 5
18    read(40,  <unfinished ...>
18    <... read resumed>"\1\0\0\0\0\0\0\0", 8) = 8
18    epoll_wait(8,  <unfinished ...>
18    <... epoll_wait resumed>[{EPOLLIN, {u32=2269396208, u64=140271606316272}}, {EPOLLIN, {u32=2269405504, u64=140271606325568}}, {EPOLLIN, {u32=3559107912, u64=140272896027976}}, {EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, 0) = 4
18    read(41, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [{EPOLLIN, {u32=2269396208, u64=140271606316272}}, {EPOLLIN, {u32=2269405504, u64=140271606325568}}, {EPOLLIN, {u32=3559107912, u64=140272896027976}}], 256, 0) = 3
18    read(36, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [{EPOLLIN, {u32=2269396208, u64=140271606316272}}, {EPOLLIN, {u32=2269405504, u64=140271606325568}}], 256, 0) = 2
18    read(38, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [{EPOLLIN, {u32=2269405504, u64=140271606325568}}], 256, 0) = 1
18    read(39, "\1\0\0\0\0\0\0\0", 8)   = 8
18    openat(AT_FDCWD, "/proc/stat", O_RDONLY) = 42
18    fstat(42, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
18    read(42, "cpu  531693681 1691808 251625926"..., 1024) = 1024
18    close(42)                         = 0
18    getpid()                          = 10
18    epoll_wait(8, [], 256, 0)         = 0
18    epoll_wait(8,  <unfinished ...>
18    <... epoll_wait resumed>[{EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, -1) = 1
18    read(41, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [], 256, 0)         = 0
18    epoll_wait(8, [{EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, -1) = 1
18    read(41, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [], 256, 0)         = 0
18    epoll_wait(8, [{EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, -1) = 1
18    read(41, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [], 256, 0)         = 0
18    epoll_wait(8, [{EPOLLIN, {u32=3559107912, u64=140272896027976}}, {EPOLLIN, {u32=2269396208, u64=140271606316272}}, {EPOLLIN, {u32=2269405504, u64=140271606325568}}, {EPOLLIN, {u32=2269406464, u64=140271606326528}}], 256, -1) = 4
18    read(41, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8, [{EPOLLIN, {u32=3559107912, u64=140272896027976}}, {EPOLLIN, {u32=2269396208, u64=140271606316272}}, {EPOLLIN, {u32=2269405504, u64=140271606325568}}], 256, 0) = 3
18    read(36, "\1\0\0\0\0\0\0\0", 8)   = 8
18    write(27, "@fD\207\223\177\0\0", 8) = 8
18    write(10, "P5E\207\223\177\0\0", 8 <unfinished ...>
18    <... write resumed>)              = 8
18    epoll_wait(8, [{EPOLLIN, {u32=2269396208, u64=140271606316272}}, {EPOLLIN, {u32=2269405504, u64=140271606325568}}, {EPOLLIN, {u32=3559108056, u64=140272896028120}}], 256, 0) = 3
18    read(38, "\1\0\0\0\0\0\0\0", 8)   = 8
18    epoll_wait(8,  <unfinished ...>
18    <... epoll_wait resumed>[{EPOLLIN, {u32=2269405504, u64=140271606325568}}, {EPOLLIN, {u32=3559108056, u64=140272896028120}}], 256, 0) = 2
18    read(39, "\1\0\0\0\0\0\0\0", 8)   = 8
18    openat(AT_FDCWD, "/proc/stat", O_RDONLY <unfinished ...>
18    <... openat resumed>)             = 42
18    fstat(42,  <unfinished ...>
18    <... fstat resumed>{st_mode=S_IFREG|0444, st_size=0, ...}) = 0
18    read(42,  <unfinished ...>
18    <... read resumed>"cpu  531693722 1691808 251625947"..., 1024) = 1024
18    close(42 <unfinished ...>
18    <... close resumed>)              = 0
18    getpid( <unfinished ...>
18    <... getpid resumed>)             = 10
18    epoll_wait(8, [{EPOLLIN, {u32=3559108056, u64=140272896028120}}, {EPOLLIN, {u32=3559483392, u64=140272896403456}}], 256, 0) = 2
18    read(9, "P5E\207\223\177\0\0", 8) = 8
18    write(2, "fatal: morestack on g0\n", 23) = 23
18    --- SIGTRAP {si_signo=SIGTRAP, si_code=SI_KERNEL} ---
18    +++ killed by SIGTRAP (core dumped) +++

What did you expect to see?

Same behavior as with GO_VERSION=1.20.

What did you see instead?

fatal: morestack on g0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 60 (40 by maintainers)

Commits related to this issue

Most upvoted comments

Have fluentbit running for 3+ days now with this patch and see no issues. Looks like the fix was good. Thank you!

Since Go 1.21, during cgo callback (calls from C to Go, which fluent-bit uses), we query pthread for the current stack bounds to set proper bounds on our stack checks when performing low-level runtime operations which need to occur on the stack Go is called with (normal goroutine execution uses a separately allocated stack).

I instrumented cgo callbacks to see how much stack space we are given, and what I’ve found is that prior to crash, we’ve been called with very little space remaining on the stack. The most extreme case I’ve found:

...
[0.687053793 P 0] M 7 procid 13 cgocallback enter g0 stack 0x7f0c8debbe90 0x7f0c8dec4290 size 0x8400 sp 0x7f0c8dec3e90 remaining size 0x8000
[0.688720318 P 0] M 7 procid 13 cgocallback exit
[0.788935339 P 0] M 7 procid 13 cgocallback enter g0 stack 0x7f0c8debbe90 0x7f0c8dec4290 size 0x8400 sp 0x7f0c8dec3e90 remaining size 0x8000
[0.893441995 P 0] M 7 procid 13 cgocallback exit
[0.894090923 P 0] M 7 procid 13 cgocallback enter g0 stack 0x7f0c8debbe90 0x7f0c8dec4290 size 0x8400 sp 0x7f0c8debbf90 remaining size 0x100
[0.894098755 P 0] M 7 procid 13 morestack on g0
...

The total stack size is 0x8400 bytes (slightly over 32KB, which seems larger than implied by https://github.com/golang/go/issues/62440#issuecomment-1707291240, but not too far off), but the remaining stack stack (difference between the lower stack bound and sp) is only 0x100 (256) bytes.

I don’t know precisely how much stack space we need (we allocate 16KB for threads created by Go, so that is the upper bound), but 256 bytes is certainly not enough!

Does fluent-bit have a minimum stack space guarantee for calls into Go? It seems that this needs more investigation on the fluent-bit side to see why there is so little stack space remaining.

P.S. Why did this work before Go 1.21? I don’t think it did. Prior to 1.21, we didn’t get the real stack bounds at all, we just assumed that there was at least 32KB. So we likely just silently overflowed the stack and corrupted whatever happened to come after.

I believe I found the bug, which is in Go.

I was thrown off by the pthread stack bound lookup we do, it turns out that isn’t relevant.

The coroutine stack allocation is at https://github.com/fluent/fluent-bit/blob/v2.1.8/lib/flb_libco/amd64.c#L142-L147. It is just malloc (the context switch is above, co_swap_function). At no point does the library change the pthread stack bounds (in hindsight, I’m not sure that this is even possible).

So when we lookup the pthread stack bounds, we will fail the bounds check at https://cs.opensource.google/go/go/+/master:src/runtime/cgocall.go;l=273-279;drc=a878d3dfa0f9d7cd1de26e3df9eb3983a9f64b53;bpv=0;bpt=1 and avoid using the pthread bounds. Instead we will use the dummy bounds computed above (assume we’re near the top of a ~32KB stack).

But if we assume a 32KB stack, why does it look like we’re called with almost no space?

The problem is the fast path at the start of stack update: https://cs.opensource.google/go/go/+/master:src/runtime/cgocall.go;l=224-227;drc=a878d3dfa0f9d7cd1de26e3df9eb3983a9f64b53;bpv=0;bpt=1

Consider this sequence:

  1. fluent-bit allocates a 16KB stack at [0x100000, 0x104000).
  2. fluent-bit calls Go with SP near the top of this stack. Let’s assume 0x103900.
  3. callbackUpdateSystemStack finds that this stack isn’t in the current stack bounds, and doesn’t match the pthread stack bounds, so it uses ~32KB dummy bounds of [0xfb900, 0x103d00).
  4. Go call completes and returns to fluent-bit.
  5. fluent-bit frees the stack.
  6. To make a new call, fluent-bit allocates a new 16KB stack, this time at [0xf9000, 0xfd000)`.
  7. fluent-bit calls Go with SP near the top of this stack. Let’s assume 0xfc900
  8. callbackUpdateSystemStack finds that this SP is within the old stack bounds, so it hits the fast path and does not change the stack bounds.

We’re now using a completely different stack allocation, but with the old stack bounds. In the old stack bounds we only appear to have 0x1000 bytes of stack space remaining.

I selected arbitrary numbers here; the overlap could of course be even smaller. It also does not depend on the fluent-bit stack being <32KB. The same could occur with larger stack sizes provided that the old stack is freed prior to subsequent, allowing the new stack to get allocated in a partially overlapping region.

I believe the right approach here is to avoid this fast path and always attempt to update the stack bounds. I will send a CL.

Hrm, thanks for confirming the repro. I will reopen.

@gopherbot please backport to 1.21. This is a fatal crash with no workaround for applications using C coroutine libraries and calling into Go. The fix here is fairly complex, so this is a medium+ risk backport, but we don’t have a simpler workaround to backport. That said, the fix has baked in post-submit for 2+ weeks without further issue.

@VirrageS could you verify that your application works properly with https://go.dev/cl/525455?

(If you aren’t familiar with building the toolchain from a CL, you can use gotip download 525455: https://pkg.go.dev/golang.org/dl/gotip)

@gopherbot Please backport to 1.21 and 1.22. This bug causes random crashes on cgocallback.

the crashstack debug handler isn’t printing

Probably this. This is because a stack switching function like systemstack isn’t CALLed by the stack frame above it. So it doesn’t make sense to unwind to the frame above it, and the unwinder doesn’t know how to do it in general.

Is it expected behavior to switch right to the end of the g0 stack allocation for calls that run on g0?

I’m not sure I understand the question. Generally, it is expected that we switch to the g0 stack wherever it is currently at. Usually (especially for a pure Go binary), there aren’t many frames on the g0 stack, so we switch to somewhere very close to the top. But if Go is called e.g. from a very deep C stack, we cannot overwrite the C frames, so we switch to right below the C frames.

Does the g0 stack need to grow, or is the SP we enter with on the g0 stack just wrong?

g0 stack doesn’t grow. We just check its bounds. So morestack on g0 is always just crashing.

For this particular bug, it is not the the SP is wrong, but we get the g0 stack bounds wrong, so it thinks it goes out of bounds.

@prattmic Nice, I was following a similar path from your stacktrace:

I see theres a stack switch from g to g0 in goroutine 18:

runtime.systemstack_switch()    
        /usr/local/go/src/runtime/asm_amd64.s:474 +0x8 fp=0xc000407a98 sp=0xc000407a88 pc=0x7f0562a07988    

Which runs on the g stack, invokes the stack switch thunk, which results in

runtime.systemstack(0xc000100008)    
        /usr/local/go/src/runtime/asm_amd64.s:509 +0x47 fp=0x7f0518778060 sp=0x7f0518778050 pc=0x7f0562a079e

being invoked on the g0 stack. If the g0 stack range is [0x7f0518777690 0x7f051877fa90] there seems to be a strange discrepancy where the g0 stack SP we switch to that is used by the runtime.systemstack routine is 0x7f0518778050. This is only leaving 0x7f0518778050 - 0x7f0518777690 = 0x9C0 (~2.5k) bytes of stack space for what looks to be garbage collection.

When looking at the stack switching thunk https://github.com/golang/go/blob/master/src/runtime/asm_amd64.s#L513 why is the saved SP ((g_sched+gobuf_sp)(DX), SP) pointing into the middle of the g0 stack? This suggests that either there are frames below runtime.systemstack that the crashstack debug handler isn’t printing, or SP was errantly saved/clobbered by previous stack management operations.

Is it expected behavior to switch right to the end of the g0 stack allocation for calls that run on g0?

This leads me to think this is a pure Go issue since this isn’t running on the FluentBit stack at exception time at all. FluentBit should have no bearing on the g0 stack size or what SP is used when switching to g0… right?

Does the g0 stack need to grow, or is the SP we enter with on the g0 stack just wrong?

I’ve run it on our normal workload now built with 1.22.2 and there really seems to be no change.

@podtserkovskiy Setting coro_stack_size to ~2MB(-s 2457600) or ~20MB(-s 24576000) doesn’t seem to have much reproducible value. I saw it run for about an hour, sometimes for ~10 minutes, and sometimes it crashes almost immediately with these values.

Heres an example backtrace:

Stack
May 09 05:32:41 fluent-bit[740296]: I0509 05:32:41.147693  740330 metrics.go:266] submitting telemetry to backend
May 09 05:33:09 fluent-bit[740296]: runtime: morestack on g0, stack [0x7f53071ba4f0 0x7f53071c28f0], sp=0x7f53071ba828, called from
May 09 05:33:09 fluent-bit[740296]: runtime.exitsyscallfast.func1()
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/proc.go:4627 +0x2c fp=0x7f53071ba830 sp=0x7f53071ba828 pc=0x7f5351f9fb8c
May 09 05:33:09 fluent-bit[740296]: runtime.systemstack(0xc000100008)
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/asm_amd64.s:509 +0x47 fp=0x7f53071ba840 sp=0x7f53071ba830 pc=0x7f5351fc7287
May 09 05:33:09 fluent-bit[740296]: fatal error: morestack on g0
May 09 05:33:09 fluent-bit[740296]: runtime stack:
May 09 05:33:09 fluent-bit[740296]: runtime.throw({0x7f535266c674?, 0x0?})
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/panic.go:1023 +0x5e fp=0xc000037f88 sp=0xc000037f58 pc=0x7f5351f93a7e
May 09 05:33:09 fluent-bit[740296]: runtime.badmorestackg0.func1()
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/proc.go:533 +0xe5 fp=0xc000037fe0 sp=0xc000037f88 pc=0x7f5351f97265
May 09 05:33:09 fluent-bit[740296]: runtime.switchToCrashStack0()
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/asm_amd64.s:559 +0x34 fp=0xc000037ff0 sp=0xc000037fe0 pc=0x7f5351fc7314
May 09 05:33:09 fluent-bit[740296]: goroutine 18 gp=0xc000007c00 m=6 mp=0xc000100008 [syscall, locked to thread]:
May 09 05:33:09 fluent-bit[740296]: runtime.goexit({})
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc0002b3fe8 sp=0xc0002b3fe0 pc=0x7f5351fc9261
May 09 05:33:09 fluent-bit[740296]: goroutine 8 gp=0xc0000061c0 m=nil [select, 5 minutes]:
May 09 05:33:09 fluent-bit[740296]: runtime.gopark(0xc000547f18?, 0x2?, 0xd0?, 0x7d?, 0xc000547ea4?)
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/proc.go:402 +0xce fp=0xc00054dd38 sp=0xc00054dd18 pc=0x7f5351f96a0e
May 09 05:33:09 fluent-bit[740296]: runtime.selectgo(0xc00054df18, 0xc000547ea0, 0x0?, 0x0, 0x0?, 0x1)
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/select.go:327 +0x725 fp=0xc00054de58 sp=0xc00054dd38 pc=0x7f5351fa7de5
May 09 05:33:09 fluent-bit[740296]: ourqueue.(*Queue).clearCounters(0xc0000764e0, 0x0?)
May 09 05:33:09 fluent-bit[740296]:         build_dir/ourqueue/queue.go:524 +0xe5 fp=0xc00054dfc0 sp=0xc00054de58 pc=0x7f535248edc5
May 09 05:33:09 fluent-bit[740296]: ourqueue.(*Queue).Heartbeat.gowrap2()
May 09 05:33:09 fluent-bit[740296]:         build_dir/ourqueue/queue.go:477 +0x25 fp=0xc00054dfe0 sp=0xc00054dfc0 pc=0x7f535248e7e5
May 09 05:33:09 fluent-bit[740296]: runtime.goexit({})
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00054dfe8 sp=0xc00054dfe0 pc=0x7f5351fc9261
May 09 05:33:09 fluent-bit[740296]: created by ourqueue.(*Queue).Heartbeat in goroutine 22
May 09 05:33:09 fluent-bit[740296]:         build_dir/ourqueue/queue.go:477 +0xcb
May 09 05:33:09 fluent-bit[740296]: goroutine 17 gp=0xc000006700 m=1 mp=0xc000050008 [syscall, 5 minutes, locked to thread]:
May 09 05:33:09 fluent-bit[740296]: runtime.goexit({})
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00005efe8 sp=0xc00005efe0 pc=0x7f5351fc9261
May 09 05:33:09 fluent-bit[740296]: goroutine 2 gp=0xc000006c40 m=nil [force gc (idle), 5 minutes]:
May 09 05:33:09 fluent-bit[740296]: runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/proc.go:402 +0xce fp=0xc00004afa8 sp=0xc00004af88 pc=0x7f5351f96a0e
May 09 05:33:09 fluent-bit[740296]: runtime.goparkunlock(...)
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/proc.go:408
May 09 05:33:09 fluent-bit[740296]: runtime.forcegchelper()
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/proc.go:326 +0xb8 fp=0xc00004afe0 sp=0xc00004afa8 pc=0x7f5351f96898
May 09 05:33:09 fluent-bit[740296]: runtime.goexit({})
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00004afe8 sp=0xc00004afe0 pc=0x7f5351fc9261
May 09 05:33:09 fluent-bit[740296]: created by runtime.init.6 in goroutine 1
May 09 05:33:09 fluent-bit[740296]:         go/1.22.2/linux_amd64/src/runtime/proc.go:314 +0x1a
May 09 05:33:09 fluent-bit[740296]: goroutine 3 gp=0xc000007180 m=nil [GC sweep wait]:

Now that there is a somewhat clear repro again, does the go team have everything it needs for reproducing, causing a crash, attaching a debugger, etc?