Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
study
提交
8678d94a
S
study
项目概览
OpenDocCN
/
study
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
study
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8678d94a
编写于
7月 03, 2019
作者:
C
codinghuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新增《PHP扩展开发》-协程-修复一些bug(一)
上级
3fc87459
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
273 addition
and
1 deletion
+273
-1
README.md
README.md
+1
-0
docs/《PHP扩展开发》-协程-修复一些bug(一).md
docs/《PHP扩展开发》-协程-修复一些bug(一).md
+262
-0
docs/《PHP扩展开发》-协程-协程getCid.md
docs/《PHP扩展开发》-协程-协程getCid.md
+2
-0
study_coroutine_util.cc
study_coroutine_util.cc
+5
-0
test.php
test.php
+3
-1
未找到文件。
README.md
浏览文件 @
8678d94a
...
...
@@ -38,3 +38,4 @@ PHP版本:7.3.5
[
16、协程getCid
](
./docs/《PHP扩展开发》-协程-协程getCid.md
)
[
17、修复一些bug(一)
](
./docs/《PHP扩展开发》-协程-修复一些bug(一).md
)
\ No newline at end of file
docs/《PHP扩展开发》-协程-修复一些bug(一).md
0 → 100644
浏览文件 @
8678d94a
# 修复一些bug(一)
开发到这里,我们需要停下来修复一些bug。我们通过具体的脚本例子来触发这个bug,然后我们通过调试分析,来修复这个bug。或许有些读者在阅读的时候发现了明显的bug,实际上,这些bug是故意留出来的,因为我们这一系列的教程是渐进式的,不会一气呵成。否则就失去了意义,那样只会和源码分析没啥区别。
OK,我们来看这个脚本:
```
php
<?php
function
task
(
$arg
)
{
$cid
=
Study\Coroutine
::
getCid
();
echo
"coroutine [
{
$cid
}
] create"
.
PHP_EOL
;
Study\Coroutine
::
yield
();
echo
"coroutine [
{
$cid
}
] create"
.
PHP_EOL
;
}
echo
"main coroutine"
.
PHP_EOL
;
$cid1
=
Study\Coroutine
::
create
(
'task'
,
'a'
);
echo
"main coroutine"
.
PHP_EOL
;
$cid2
=
Study\Coroutine
::
create
(
'task'
,
'b'
);
echo
"main coroutine"
.
PHP_EOL
;
Study\Coroutine
::
resume
(
$cid1
);
echo
"main coroutine"
.
PHP_EOL
;
Study\Coroutine
::
resume
(
$cid2
);
echo
"main coroutine"
.
PHP_EOL
;
Study\Coroutine
::
resume
(
$cid2
);
```
和之前脚本的区别在于最后几行,我调用了2次
`resume($cid2)`
。
OK,我们来执行一下脚本:
```
shell
~/codeDir/cppCode/study
# php test.php
main coroutine
coroutine
[
1] create
main coroutine
coroutine
[
2] create
main coroutine
coroutine
[
1] create
main coroutine
coroutine
[
2] create
main coroutine
Segmentation fault
~/codeDir/cppCode/study
#
```
我们发现,报了一错
`Segmentation fault`
。
我们来调试一下:
```
shell
~/codeDir/cppCode/study
# cgdb php
GNU gdb
(
GDB
)
8.2
Copyright
(
C
)
2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type
"show copying"
and
"show warranty"
for
details.
This GDB was configured as
"x86_64-alpine-linux-musl"
.
Type
"show configuration"
for
configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For
help
,
type
"help"
.
Type
"apropos word"
to search
for
commands related to
"word"
...
Reading symbols from php...
(
no debugging symbols found
)
...done.
(
gdb
)
```
然后执行脚本:
```
shell
(
gdb
)
r test.php
This GDB was configured as
"x86_64-alpine-linux-musl"
.
Type
"show configuration"
for
configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For
help
,
type
"help"
.
Type
"apropos word"
to search
for
commands related to
"word"
...
Reading symbols from php...
(
no debugging symbols found
)
...done.
(
gdb
)
r test.php
Starting program: /usr/local/bin/php test.php
main coroutine
coroutine
[
1] create
main coroutine
coroutine
[
2] create
main coroutine
coroutine
[
1] create
main coroutine
coroutine
[
2] create
main coroutine
Program received signal SIGSEGV, Segmentation fault.
zim_study_coroutine_util_resume
(
execute_data
=
0x7ffff761d110,
return_value
=
0x7fffffffb0b0
)
at /root/codeDir/cppCode/study/study_coroutine_util.cc:54
(
gdb
)
```
我们发现报错地方是
`study_coroutine_util.cc:54`
。对应的代码是:
```
cpp
Coroutine
*
co
=
coroutine_iterator
->
second
;
```
很容易想到问题肯定是出在了
`coroutine_iterator->second`
上面。
OK,我们在函数
`zim_study_coroutine_util_resume`
打一个断点:
```
cpp
(
gdb
)
b
zim_study_coroutine_util_resume
Breakpoint
1
at
0x7ffff78d1b8a
:
file
/
root
/
codeDir
/
cppCode
/
study
/
study_coroutine_util
.
cc
,
line
45.
(
gdb
)
```
然后我们重新执行脚本:
```
shell
(
gdb
)
r test.php
The program being debugged has been started already.
Start it from the beginning?
(
y or n
)
y
The program being debugged has been started already.
Starting program: /usr/local/bin/php test.php
main coroutine
coroutine
[
1] create
main coroutine
coroutine
[
2] create
main coroutine
Breakpoint 1, zim_study_coroutine_util_resume
(
execute_data
=
0x7ffff761d110,
return_value
=
0x7fffffffb0b0
)
at /root/codeDir/cppCode/study/study_coroutine_util.cc:45
(
gdb
)
```
我们继续执行:
```
shell
(
gdb
)
c
coroutine
[
1] create
main coroutine
Continuing.
Breakpoint 1, zim_study_coroutine_util_resume
(
execute_data
=
0x7ffff761d110,
return_value
=
0x7fffffffb0b0
)
at /root/codeDir/cppCode/study/study_coroutine_util.cc:45
(
gdb
)
```
我们再继续执行:
```
shell
(
gdb
)
c
Continuing.
coroutine
[
2] create
main coroutine
Breakpoint 1, zim_study_coroutine_util_resume
(
execute_data
=
0x7ffff761d110,
return_value
=
0x7fffffffb0b0
)
at /root/codeDir/cppCode/study/study_coroutine_util.cc:45
(
gdb
)
```
```
shell
44│ PHP_METHOD
(
study_coroutine_util, resume
)
45├>
{
46│ zend_long cid
=
0
;
47│
48│ ZEND_PARSE_PARAMETERS_START
(
1, 1
)
49│ Z_PARAM_LONG
(
cid
)
50│ ZEND_PARSE_PARAMETERS_END_EX
(
RETURN_FALSE
)
;
51│
52│ auto coroutine_iterator
=
user_yield_coros.find
(
cid
)
;
53│
54│ Coroutine
*
co
=
coroutine_iterator->second
;
55│ user_yield_coros.erase
(
cid
)
;
56│ co->resume
()
;
57│ RETURN_TRUE
;
```
OK,这个时候,停在了最后一行代码:
```
php
Study\Coroutine
::
resume
(
$cid2
);
```
里面了。
我们执行到
`54`
行之前(不会执行54行的代码),这是报错的地方:
```
shell
(
gdb
)
u 54
zim_study_coroutine_util_resume
(
execute_data
=
0x7ffff761d110,
return_value
=
0x7fffffffb0b0
)
at /root/codeDir/cppCode/study/study_coroutine_util.cc:54
(
gdb
)
```
```
shell
52│ auto coroutine_iterator
=
user_yield_coros.find
(
cid
)
;
53│
54├───> Coroutine
*
co
=
coroutine_iterator->second
;
55│ user_yield_coros.erase
(
cid
)
;
56│ co->resume
()
;
57│ RETURN_TRUE
;
58│
}
```
此时,我们打印
`coroutine_iterator`
的值:
```
shell
(
gdb
)
p coroutine_iterator
$2
=
{
<std::__detail::_Node_iterator_base<std::pair<long const, Study::Coroutine
*
>
,
false
>>
=
{
_M_cur
=
0x0
}
, <No data fields>
}
(
gdb
)
```
发现
`_M_cur = 0x0`
,所以是没有取出东西来。
没有取出东西来的原因是我们在每次
`resume`
一个协程的时候,会把对应的协程从
`user_yield_coros`
里面删除,所以我们接着第二次
`resume`
这个协程的时候,就无法找到这个协程了。除非在第二次
`resume`
这个协程之前,又
`yield`
这个协程了,重新把这个协程添加进去了
`user_yield_coros`
里面。
所以,这里我们需要对
`coroutine_iterator`
进行判断。我们修改一下
`PHP_METHOD(study_coroutine_util, resume)`
这个接口:
```
cpp
if
(
coroutine_iterator
==
user_yield_coros
.
end
())
{
php_error_docref
(
NULL
,
E_WARNING
,
"resume error"
);
RETURN_FALSE
;
}
```
OK,我们编译、安装:
```
shell
make clean
&&
make
&&
make
install
```
然后在执行脚本:
```
shell
~/codeDir/cppCode/study
# php test.php
main coroutine
coroutine
[
1] create
main coroutine
coroutine
[
2] create
main coroutine
coroutine
[
1] create
main coroutine
coroutine
[
2] create
main coroutine
Warning: Study
\C
oroutine::resume
()
: resume error
in
/root/codeDir/cppCode/study/test.php on line 21
~/codeDir/cppCode/study
#
```
OK,我们成功的报出了
`Warning`
,不至于让脚本因为无法
`resume`
一个协程而停止执行
docs/《PHP扩展开发》-协程-协程getCid.md
浏览文件 @
8678d94a
...
...
@@ -154,6 +154,8 @@ main coroutine
符合预期。
[
下一篇:修复一些bug(一)
](
./《PHP扩展开发》-协程-修复一些bug(一).md
)
...
...
study_coroutine_util.cc
浏览文件 @
8678d94a
...
...
@@ -50,6 +50,11 @@ PHP_METHOD(study_coroutine_util, resume)
ZEND_PARSE_PARAMETERS_END_EX
(
RETURN_FALSE
);
auto
coroutine_iterator
=
user_yield_coros
.
find
(
cid
);
if
(
coroutine_iterator
==
user_yield_coros
.
end
())
{
php_error_docref
(
NULL
,
E_WARNING
,
"resume error"
);
RETURN_FALSE
;
}
Coroutine
*
co
=
coroutine_iterator
->
second
;
user_yield_coros
.
erase
(
cid
);
...
...
test.php
浏览文件 @
8678d94a
...
...
@@ -17,4 +17,6 @@ echo "main coroutine" . PHP_EOL;
Study\Coroutine
::
resume
(
$cid1
);
echo
"main coroutine"
.
PHP_EOL
;
Study\Coroutine
::
resume
(
$cid2
);
echo
"main coroutine"
.
PHP_EOL
;
\ No newline at end of file
echo
"main coroutine"
.
PHP_EOL
;
Study\Coroutine
::
resume
(
$cid2
);
echo
"main coroutine"
.
PHP_EOL
;
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录