yyzybb537 / libgo

Go-style concurrency in C++11
MIT License
3.22k stars 758 forks source link

版本3 以下测试 segmentation fault #116

Closed zgww closed 6 years ago

zgww commented 6 years ago

#include <stdio.h>
#include <string.h>
#include <memory>
#include <libgo/libgo.h>
#include <unistd.h>

#include <chrono>
#include <vector>
#include <thread>
#include <unordered_map>

using namespace std;

void action(int i){
    printf("go action i=%d\n", i);
    for (int times = 0; ; times++){
        printf("go-action-%d times=%d\n", i, times);
        sleep(i % 5);
    }
}

int main(int argc, char **argv){
    go []{
        for (int i = 0; i < 1000; i++){
            go [=] {
                action(i);
            };
        }
    };
    co_sched.Start(6); 
    return 0;
}

测试环境: 阿里云Ubuntu16.04 libgo版本:

commit d31a8353e12f5a66d2f6655b48f96cf178e62561
Author: yuyangzi <yuyangzi@gmail.com>
Date:   Wed Nov 7 11:20:15 2018 +0800

    编写cpu密集型程序时, 可以延长协程执行的超时判断阈值, 避免频繁的worksteal产生

编译参数 g++-5 -std=c++11 -g .obj/test/go.o -L/usr/local/lib -llibgo -ldl -lpthread -o bin/go 运行1,2分钟后崩溃,报segmentation fault

core文件:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./bin/go'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000022f0e38 in ?? ()
[Current thread is 1 (Thread 0x7fd557995700 (LWP 18894))]
(gdb) bt
#0  0x00000000022f0e38 in ?? ()
#1  0x00007fd4fd050ca0 in ?? ()
#2  0x000000000040b650 in co::RefObjectImpl::DecrementWeak (this=0x0) at /home/zgww/lib/libgo/libgo/scheduler/../task/../common/util.h:94
#3  0x000000000042b20f in co::Context::SwapOut (this=0x22f0db0)
    at /home/zgww/lib/libgo/libgo/netio/unix/../../scheduler/../task/../context/context.h:46
#4  co::Task::SwapOut (this=0x22f0d50) at /home/zgww/lib/libgo/libgo/netio/unix/../../scheduler/../task/task.h:53
#5  co::Processer::CoYield (this=0x2286df0) at /home/zgww/lib/libgo/libgo/netio/unix/../../scheduler/processer.h:179
#6  co::Processer::StaticCoYield () at /home/zgww/lib/libgo/libgo/netio/unix/../../scheduler/processer.h:163
#7  sleep (seconds=0) at /home/zgww/lib/libgo/libgo/netio/unix/hook.cpp:781
#8  0x00000000004041cc in action (i=930) at test/go.cpp:18
#9  0x00000000004041eb in <lambda()>::<lambda()>::operator()(void) const (__closure=0x22f0dd8) at test/go.cpp:26
#10 0x00000000004046d2 in std::_Function_handler<void(), main(int, char**)::<lambda()>::<lambda()> >::_M_invoke(const std::_Any_data &) (
    __functor=...) at /usr/include/c++/5/functional:1871
#11 0x0000000000423874 in std::function<void ()>::operator()() const (this=0x22f0dd8) at /usr/include/c++/5/functional:2267
#12 0x0000000000442427 in co::Task::<lambda()>::operator()(void) const (__closure=0x7fd4fd050f10)
    at /home/zgww/lib/libgo/libgo/task/task.cpp:37
#13 0x000000000044255d in co::Task::Run (this=0x22f0d50) at /home/zgww/lib/libgo/libgo/task/task.cpp:48
#14 0x000000000044299e in co::Task::StaticRun (vp=36638032) at /home/zgww/lib/libgo/libgo/task/task.cpp:79
#15 0x000000000044a461 in make_fcontext () at /home/zgww/lib/libgo/libgo/context/make_x86_64_sysv_elf_gas.S:64
#16 0x0000000000000000 in ?? ()

test/go.cpp:18 行:

        sleep(i % 5);
yyzybb537 commented 6 years ago

git branch -av看一下是哪个commit?

zgww commented 6 years ago
* master                     d31a835 编写cpu密集型程序时, 可以延长协程执行的超时判断阈值, 避免频繁的worksteal产生
  remotes/origin/3.0         98b3e49 add version define
  remotes/origin/HEAD        -> origin/master
  remotes/origin/dev         a7fa54d 测试代码
  remotes/origin/dev-win     813a936 修复overlapped没有置空导致WSAxx必定失败的bug
  remotes/origin/mac         af93212 Merge pull request #29 from eagle518/master
  remotes/origin/master      d31a835 编写cpu密集型程序时, 可以延长协程执行的超时判断阈值, 避免频繁的worksteal产生
  remotes/origin/ring-switch 6dbd863 环形切换
  remotes/origin/windows     ee13fe4 调整cmake文件, 适应vcpkg
yyzybb537 commented 6 years ago

嗯 看起来是有什么问题导致协程栈被写坏了。。。

yyzybb537 commented 6 years ago

bug已修复: block后唤醒的协程应该加入到runnableQueue中, 之前搞错了加到newQueue中了, 这就导致还没有swapOut的时候就被steal到其他线程去调度.