Skip to content

Commit

Permalink
Fix #3792 and add a test for it (#3793)
Browse files Browse the repository at this point in the history
  • Loading branch information
twose authored Oct 31, 2020
1 parent abd3887 commit a8e11b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/coroutine/hook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ using swoole::coroutine::System;

static std::unordered_map<int, Socket *> socket_map;
static std::mutex socket_map_lock;
static thread_local struct dirent _tmp_dirent;

static sw_inline bool is_no_coro() {
return SwooleTG.reactor == nullptr || !Coroutine::get_current();
Expand Down Expand Up @@ -337,15 +336,10 @@ struct dirent *swoole_coroutine_readdir(DIR *dirp) {
return readdir(dirp);
}

struct dirent *retval = &_tmp_dirent;
struct dirent *retval;

swoole::coroutine::async([&retval, dirp]() {
struct dirent *tmp = readdir(dirp);
if (tmp) {
memcpy(retval, tmp, sizeof(*tmp));
} else {
retval = nullptr;
}
retval = readdir(dirp);
});

return retval;
Expand Down
44 changes: 44 additions & 0 deletions tests/swoole_runtime/file_hook/bug_scandir.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
swoole_runtime/file_hook: fseek ftell file larger than 2G bug
--SKIPIF--
<?php
require __DIR__ . '/../../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../../include/bootstrap.php';

$testDir = sys_get_temp_dir() . '/swoole_scandir_bug';

if (!is_dir($testDir)) {
mkdir($testDir);
}
for ($i = 0; $i++ < 3;) {
touch("{$testDir}/{$i}.txt");
}

\Swoole\Runtime::enableCoroutine(true);
\Swoole\Coroutine\run(
function () use ($testDir) {
for ($i = 0; $i < MAX_CONCURRENCY; $i++) {
go(
function () use ($testDir) {
$files = scandir($testDir);
Assert::same($files, [
'.',
'..',
'1.txt',
'2.txt',
'3.txt',
]);
}
);
}
}
);

echo "DONE\n";

?>
--EXPECT--
DONE

0 comments on commit a8e11b9

Please sign in to comment.