{"id":554,"date":"2025-04-15T17:40:44","date_gmt":"2025-04-15T09:40:44","guid":{"rendered":"http:\/\/120.55.184.7\/?p=554"},"modified":"2025-05-03T05:40:26","modified_gmt":"2025-05-02T21:40:26","slug":"bug%e8%ae%b0%e5%bd%95%ef%bc%9a%e6%ad%bb%e9%94%811","status":"publish","type":"post","link":"https:\/\/beijian99.top\/?p=554","title":{"rendered":"[BUG]\u6b7b\u95011"},"content":{"rendered":"\n<p>\u6700\u8fd1\u65b0\u5f00\u53d1\u4e86\u4e2a\u6392\u884c\u699c\u7cfb\u7edf\uff0c\u538b\u529b\u6d4b\u8bd5\u65f6\u9ad8\u5e76\u53d1\u62c9\u53d6\u699c\u5355\u65f6\uff0c\u4f1a\u5bfc\u81f4\u6574\u4e2a\u670d\u52a1\u4e0d\u53ef\u7528\u3002\u7528pprof\u67e5\u770b\u60c5\u51b5\uff0c\u53d1\u73b0\u591a\u4e2agoroutine\u957f\u65f6\u95f4\u963b\u585e\uff0c\u4e0e\u5176\u76f8\u5173\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\n\n\/\/ Worker \u5b9a\u4e49\u5982\u4f55\u6267\u884c ListenMsg \u65f6\u8bbe\u7f6e\u7684\u56de\u8c03\u51fd\u6570\ntype Worker interface {\n\t\/\/ Post \u6267\u884c\u5de5\u4f5c\u4efb\u52a1\n\tPost(func())\n\n\t\/\/ WaitDone \u7b49\u5f85\u5f53\u524d\u6240\u6709\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\n\tWaitDone()\n\n\tGoroutineCount() int32\n}\n\ntype parallelWorker struct {\n\twg             sync.WaitGroup\n\tch             chan struct{}\n\trecover        bool\n\tgoroutineCount int32\n}\n\nfunc (w *parallelWorker) GoroutineCount() int32 {\n\treturn atomic.LoadInt32(&amp;w.goroutineCount)\n}\n\nfunc (w *parallelWorker) Post(f func()) {\n\tw.wg.Add(1)\n\tatomic.AddInt32(&amp;w.goroutineCount, 1)\n\tif w.ch == nil {\n\t\tgo func() {\n\t\t\tdefer func() {\n\t\t\t\tatomic.AddInt32(&amp;w.goroutineCount, -1)\n\t\t\t\tw.wg.Done()\n\t\t\t\tif w.recover {\n\t\t\t\t\tif err := recover(); err != nil {\n\t\t\t\t\t\tstack := debug.Stack()\n\t\t\t\t\t\tlogrus.WithFields(logrus.Fields{\n\t\t\t\t\t\t\t&quot;err&quot;:   err,\n\t\t\t\t\t\t\t&quot;stack&quot;: string(stack),\n\t\t\t\t\t\t}).Error(&quot;Recover&quot;)\n\n\t\t\t\t\t\tos.Stderr.Write(&#x5B;]byte(fmt.Sprintf(&quot;%v\\n&quot;, err)))\n\t\t\t\t\t\tos.Stderr.Write(stack)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}()\n\t\t\tf()\n\t\t}()\n\t} else { \/\/ goroutine\u6570\u91cf\u9650\u5236,\u901a\u8fc7channel\u63a7\u5236\n\t\t\/\/ \u4ece\u4fe1\u9053\u4e2d\u53d6\u4e00\u4e2astruct{}\u3002\u5982\u679c\u4fe1\u9053\u4e3a\u7a7a\uff0c\u8bf4\u660e\u65e0\u53ef\u7528goroutine\uff0c\u90fd\u5df2\u88ab\u5360\u7528\uff0c\u6b64\u65f6\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u6709goroutine\u88ab\u91ca\u653e\n\t\t&lt;-w.ch\n\t\tgo func() {\n\t\t\tdefer func() {\n\t\t\t\tatomic.AddInt32(&amp;w.goroutineCount, -1)\n\t\t\t\tw.wg.Done()\n\t\t\t\tw.ch &lt;- struct{}{} \/\/ \u5f52\u8fd8\u4e00\u4e2astruct{} \u5373\u89e3\u9664\u5bf9\u5f53\u524d\u6301\u6709\u7684goroutine\u7684\u5360\u7528\n\t\t\t\tif w.recover {\n\t\t\t\t\tif err := recover(); err != nil {\n\t\t\t\t\t\tstack := debug.Stack()\n\t\t\t\t\t\tlogrus.WithFields(logrus.Fields{\n\t\t\t\t\t\t\t&quot;err&quot;:   err,\n\t\t\t\t\t\t\t&quot;stack&quot;: string(stack),\n\t\t\t\t\t\t}).Error(&quot;Recover&quot;)\n\n\t\t\t\t\t\tos.Stderr.Write(&#x5B;]byte(fmt.Sprintf(&quot;%v\\n&quot;, err)))\n\t\t\t\t\t\tos.Stderr.Write(stack)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}()\n\t\t\tf()\n\t\t}()\n\t}\n}\nfunc (w *parallelWorker) WaitDone() {\n\tw.wg.Wait()\n}\n\n\/\/ NewParallelWorker \u521b\u5efa\u5e76\u53d1\u7684\u6267\u884c\u5668.\n\/\/ maxGoroutines \u4e3a\u6700\u5927 goroutine \u5e76\u53d1\u6570\u91cf, maxGoroutines &lt;= 0 \u65f6, \u8868\u793a\u4e0d\u9650.\n\/\/ recover \u8868\u793a\u662f\u5426\u9700\u8981\u542f\u7528 recover \u6765\u4fdd\u62a4 panic.\nfunc NewParallelWorker(maxGoroutines int, recover bool) Worker {\n\tw := &amp;parallelWorker{recover: recover}\n\tif maxGoroutines &gt; 0 {\n\t\tw.ch = make(chan struct{}, maxGoroutines)\n\t\tfor i := 0; i &lt; maxGoroutines; i++ {\n\t\t\t\/\/ \u5411\u7f13\u51b2\u4fe1\u9053\u4e2d\u5199\u5165 maxGoroutines \u4e2a\u4fe1\u53f7\uff0c\u4ee3\u8868\u521d\u59cb\u65f6\u6709maxGoroutines \u4e2a goroutine\u53ef\u7528\n\t\t\tw.ch &lt;- struct{}{}\n\t\t}\n\t}\n\treturn w\n}\n<\/pre><\/div>\n\n\n<h1 class=\"wp-block-heading\">ParallelWorker \u4ee3\u7801\u5206\u6790\u4e0e\u6b7b\u9501\u60c5\u51b5\u5206\u6790<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ee3\u7801\u8be6\u89e3<\/h2>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5e76\u884c\u4efb\u52a1\u6267\u884c\u5668\uff0c\u4e3b\u8981\u529f\u80fd\u662f\u63a7\u5236\u5e76\u53d1\u6267\u884c\u7684goroutine\u6570\u91cf\uff0c\u5e76\u63d0\u4f9b\u4efb\u52a1\u63d0\u4ea4\u548c\u7b49\u5f85\u5b8c\u6210\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6838\u5fc3\u7ed3\u6784<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Worker<\/code>\u63a5\u53e3\u5b9a\u4e49\u4e86\u4e09\u4e2a\u65b9\u6cd5\uff1a<code>Post<\/code>(\u63d0\u4ea4\u4efb\u52a1)\u3001<code>WaitDone<\/code>(\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210)\u548c<code>GoroutineCount<\/code>(\u83b7\u53d6\u5f53\u524dgoroutine\u6570\u91cf)<\/li>\n\n\n\n<li><code>parallelWorker<\/code>\u5b9e\u73b0\u4e86<code>Worker<\/code>\u63a5\u53e3\uff0c\u5305\u542b\uff1a<br>\u2022 <code>wg<\/code>: <code>sync.WaitGroup<\/code>\u7528\u4e8e\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210<br>\u2022 <code>ch<\/code>: \u5e26\u7f13\u51b2\u7684channel\uff0c\u7528\u4e8e\u63a7\u5236\u6700\u5927\u5e76\u53d1goroutine\u6570\u91cf<br>\u2022 <code>recover<\/code>: \u662f\u5426\u542f\u7528panic\u6062\u590d<br>\u2022 <code>goroutineCount<\/code>: \u5f53\u524d\u6d3b\u8dc3\u7684goroutine\u6570\u91cf(\u539f\u5b50\u8ba1\u6570\u5668)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u952e\u65b9\u6cd5<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>Post(func())<\/code>:<br>\u2022 \u5f53<code>ch<\/code>\u4e3anil\u65f6(\u65e0\u9650\u5236\u6a21\u5f0f)\uff0c\u76f4\u63a5\u542f\u52a8goroutine\u6267\u884c\u4efb\u52a1<br>\u2022 \u5f53<code>ch<\/code>\u975enil\u65f6(\u9650\u5236\u6a21\u5f0f)\uff0c\u5148\u4ecechannel\u83b7\u53d6\u4ee4\u724c\uff0c\u518d\u542f\u52a8goroutine<br>\u2022 \u4e24\u79cd\u6a21\u5f0f\u90fd\u5305\u542bpanic\u6062\u590d\u903b\u8f91(\u5982\u679c\u542f\u7528)<\/li>\n\n\n\n<li><code>WaitDone()<\/code>:<br>\u2022 \u4f7f\u7528<code>wg.Wait()<\/code>\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210<\/li>\n\n\n\n<li><code>NewParallelWorker(maxGoroutines int, recover bool)<\/code>:<br>\u2022 \u521b\u5efa\u5e76\u521d\u59cb\u5316<code>parallelWorker<\/code><br>\u2022 \u5f53<code>maxGoroutines>0<\/code>\u65f6\uff0c\u521d\u59cb\u5316\u5e26\u7f13\u51b2\u7684channel\u5e76\u586b\u5145\u4ee4\u724c<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u6b7b\u9501\u98ce\u9669\u5206\u6790(\u5f53maxGoroutines=5\u65f6)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6b7b\u9501\u573a\u666f<\/h3>\n\n\n\n<p>\u5f53\u4e1a\u52a1\u8c03\u7528\u540c\u6b65\u963b\u585e\u7684RPC\u65f6\uff0c\u53ef\u80fd\u51fa\u73b0\u4ee5\u4e0b\u6b7b\u9501\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6240\u6709goroutine\u88ab\u5360\u7528<\/strong>\uff1a<br>\u2022 5\u4e2agoroutine\u90fd\u5728\u6267\u884c\u540c\u6b65\u963b\u585e\u7684RPC\u8c03\u7528<br>\u2022 \u7b2c6\u4e2a\u4efb\u52a1\u8c03\u7528<code>Post()<\/code>\u65f6\uff0c\u4f1a\u963b\u585e\u5728<code>&lt;-w.ch<\/code>\u7b49\u5f85\u53ef\u7528goroutine<\/li>\n\n\n\n<li><strong>\u4efb\u52a1\u95f4\u4f9d\u8d56<\/strong>\uff1a<br>\u2022 \u5982\u679c\u67d0\u4e2a\u4efb\u52a1\u7684\u5b8c\u6210\u4f9d\u8d56\u4e8e\u53e6\u4e00\u4e2a\u88ab\u963b\u585e\u7684\u4efb\u52a1(\u5982\u7b49\u5f85\u5176RPC\u7ed3\u679c)<br>\u2022 \u4f46\u8be5\u4efb\u52a1\u56e0\u6ca1\u6709\u53ef\u7528goroutine\u800c\u65e0\u6cd5\u6267\u884c<br>\u2022 \u5f62\u6210\u5faa\u73af\u7b49\u5f85<\/li>\n\n\n\n<li><strong>RPC\u8c03\u7528\u5185\u90e8\u4f7f\u7528\u540c\u4e00Worker<\/strong>\uff1a<br>\u2022 \u5982\u679cRPC\u5904\u7406\u8fc7\u7a0b\u4e2d\u53c8\u4f7f\u7528\u540c\u4e00\u4e2aWorker\u63d0\u4ea4\u65b0\u4efb\u52a1<br>\u2022 \u53ef\u80fd\u5bfc\u81f4\u6240\u6709goroutine\u90fd\u5728\u7b49\u5f85\u65b0\u4efb\u52a1\u6267\u884c\uff0c\u800c\u65b0\u4efb\u52a1\u53c8\u9700\u8981goroutine<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u5177\u4f53\u6b7b\u9501\u6761\u4ef6<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6240\u67095\u4e2agoroutine\u90fd\u5728\u6267\u884c\u540c\u6b65\u963b\u585e\u64cd\u4f5c<\/li>\n\n\n\n<li>\u8fd9\u4e9b\u963b\u585e\u64cd\u4f5c\u9700\u8981\u7b49\u5f85\u5176\u4ed6\u4efb\u52a1\u5b8c\u6210<\/li>\n\n\n\n<li>\u4f46\u5176\u4ed6\u4efb\u52a1\u56e0\u6ca1\u6709\u53ef\u7528goroutine\u800c\u65e0\u6cd5\u6267\u884c<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Bug\u590d\u73b0\u4ee3\u7801<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\nworker := NewParallelWorker(5, true)\n\n\/\/ \u63d0\u4ea45\u4e2a\u963b\u585e\u4efb\u52a1\nfor i := 0; i &lt; 5; i++ {\n    worker.Post(func() {\n        \/\/ \u540c\u6b65\u963b\u585eRPC\u8c03\u7528\n        result := makeSomeRPCCall() \n\n        \/\/ \u5728RPC\u56de\u8c03\u4e2d\u53c8\u63d0\u4ea4\u65b0\u4efb\u52a1\n        worker.Post(func() {\n            fmt.Println(&quot;This will deadlock!&quot;)\n        })\n    })\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u5728\u53d7\u9650Worker\u4e2d\u6267\u884c\u540c\u6b65\u963b\u585e\u64cd\u4f5c<\/strong>\uff1a<br>\u2022 \u5c06\u540c\u6b65RPC\u6539\u4e3a\u5f02\u6b65\u65b9\u5f0f<br>\u2022 \u4f7f\u7528\u56de\u8c03\u6216channel\u5904\u7406\u7ed3\u679c<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u66f4\u5927\u7684goroutine\u9650\u5236<\/strong>\uff1a<br>\u2022 \u6839\u636e\u4e1a\u52a1\u9700\u6c42\u8c03\u6574maxGoroutines\u503c<\/li>\n\n\n\n<li><strong>\u5206\u5c42Worker<\/strong>\uff1a<br>\u2022 \u4e3aRPC\u8c03\u7528\u4f7f\u7528\u5355\u72ec\u7684Worker\u5b9e\u4f8b<br>\u2022 \u907f\u514d\u4efb\u52a1\u95f4\u7684\u76f8\u4e92\u963b\u585e<\/li>\n\n\n\n<li><strong>\u8d85\u65f6\u673a\u5236<\/strong>\uff1a<br>\u2022 \u4e3a\u963b\u585e\u64cd\u4f5c\u6dfb\u52a0\u8d85\u65f6\u63a7\u5236<br>\u2022 \u9632\u6b62\u65e0\u9650\u671f\u7b49\u5f85<\/li>\n\n\n\n<li><strong>\u4efb\u52a1\u961f\u5217\u5206\u79bb<\/strong>\uff1a<br>\u2022 \u5c06\u53ef\u80fd\u963b\u585e\u7684\u4efb\u52a1\u4e0e\u5176\u4ed6\u4efb\u52a1\u5206\u5f00\u5904\u7406<br>\u2022 \u4f7f\u7528\u4e0d\u540c\u7684Worker\u5b9e\u4f8b<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u65b0\u5f00\u53d1\u4e86\u4e2a\u6392\u884c\u699c\u7cfb\u7edf\uff0c\u538b\u529b\u6d4b\u8bd5\u65f6\u9ad8\u5e76\u53d1\u62c9\u53d6\u699c\u5355\u65f6\uff0c\u4f1a\u5bfc\u81f4\u6574\u4e2a\u670d\u52a1\u4e0d\u53ef\u7528\u3002\u7528pprof\u67e5\u770b\u60c5\u51b5\uff0c\u53d1\u73b0\u591a\u4e2ago [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":965,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[131],"tags":[140],"class_list":["post-554","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-131","tag-bug"],"_links":{"self":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=554"}],"version-history":[{"count":3,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/554\/revisions"}],"predecessor-version":[{"id":706,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/554\/revisions\/706"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/media\/965"}],"wp:attachment":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}