Skip to content

Commit

Permalink
Fixed bug that call to a member function getArrayCopy() on null when …
Browse files Browse the repository at this point in the history
…the parent coroutine context destroyed. (#3106)

* Fixed copy failed when the parent coroutine context destroyed.

* Update CHANGELOG-2.1.md
  • Loading branch information
limingxinleo authored Jan 11, 2021
1 parent d2ab897 commit 17a4e2f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# v2.1.3 - TBD

## Fixed

- [#3106](https://github.com/hyperf/hyperf/pull/3106) Fixed bug that call to a member function getArrayCopy() on null when the parent coroutine context destroyed.

# v2.1.2 - 2021-01-11

## Fixed
Expand Down
6 changes: 4 additions & 2 deletions src/utils/src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ public static function destroy(string $id)
*/
public static function copy(int $fromCoroutineId, array $keys = []): void
{
/** @var \ArrayObject $from */
$from = Co::getContextFor($fromCoroutineId);
/** @var \ArrayObject $current */
if ($from === null) {
return;
}

$current = Co::getContextFor();
$current->exchangeArray($keys ? Arr::only($from->getArrayCopy(), $keys) : $from->getArrayCopy());
}
Expand Down
19 changes: 19 additions & 0 deletions src/utils/tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,23 @@ public function testContextChangeAfterCopy()

$this->assertSame($tid, Context::get('test.store.id')->id);
}

public function testContextFromNull()
{
$res = Context::get('id', $default = 'Hello World!', -1);
$this->assertSame($default, $res);

$res = Context::get('id', null, -1);
$this->assertSame(null, $res);

$this->assertFalse(Context::has('id', -1));

Context::copy(-1);

parallel([function () {
Context::set('id', $id = uniqid());
Context::copy(-1, ['id']);
$this->assertSame($id, Context::get('id'));
}]);
}
}

0 comments on commit 17a4e2f

Please sign in to comment.