Skip to content

Commit

Permalink
Add thread-safe lock for _state and _*_handler's modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Jul 16, 2020
1 parent ab28d36 commit a57cd4c
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 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 All @@ -243,9 +246,10 @@ def _fulfill(self, value):

def _reject(self, reason, traceback=None):
# type: (Exception, Optional[TracebackType]) -> None
self._state = STATE_REJECTED
self._fulfillment_handler0 = reason
self._traceback = traceback
with _state_lock:
self._state = STATE_REJECTED
self._fulfillment_handler0 = reason
self._traceback = traceback

if self._is_final:
assert self._length == 0
Expand Down Expand Up @@ -507,13 +511,13 @@ def _wait(self, timeout=None):

def get(self, timeout=None):
# type: (Optional[float]) -> T
target = self._target()
self._wait(timeout or DEFAULT_TIMEOUT)
return self._target_settled_value(_raise=True)

def _target_settled_value(self, _raise=False):
# type: (bool) -> Any
return self._target()._settled_value(_raise)
with _state_lock:
return self._target()._settled_value(_raise)

_value = _reason = _target_settled_value
value = reason = property(_target_settled_value)
Expand Down Expand Up @@ -570,12 +574,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 a57cd4c

Please sign in to comment.