Skip to content

Commit

Permalink
Add thread-safe lock for _state and _rejection_handler0 modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Jul 15, 2020
1 parent 35b9845 commit 7ae2c1c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions promise/promise.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import namedtuple
from functools import partial, wraps
from sys import version_info, exc_info
from threading import RLock
from threading import Lock
from types import TracebackType
from weakref import WeakKeyDictionary

Expand Down Expand Up @@ -43,6 +43,8 @@

async_instance = Async()

_state_lock = Lock()


def get_default_scheduler():
# type: () -> ImmediateScheduler
Expand Down Expand Up @@ -232,8 +234,9 @@ def _fulfill(self, value):
err = make_self_resolution_error()
# self._attach_extratrace(err)
return self._reject(err)
self._state = STATE_FULFILLED
self._rejection_handler0 = value
with _state_lock:
self._state = STATE_FULFILLED
self._rejection_handler0 = value

if self._length > 0:
if self._is_async_guaranteed:
Expand Down Expand Up @@ -570,12 +573,14 @@ def _then(
):
# type: (...) -> Promise[S]
promise = self.__class__() # type: Promise
target = self._target()

state = target._state
if state == STATE_PENDING:
target._add_callbacks(did_fulfill, did_reject, promise)
else:
with _state_lock:
target = self._target()
state = target._state
if state == STATE_PENDING:
target._add_callbacks(did_fulfill, did_reject, promise)

if state != STATE_PENDING:
traceback = None
if state == STATE_FULFILLED:
value = target._rejection_handler0
Expand Down

0 comments on commit 7ae2c1c

Please sign in to comment.