Skip to content

Commit

Permalink
Make CancellationToken.wait only wait for a limited time (#300)
Browse files Browse the repository at this point in the history
`Event` didn't have this problem, since it assumed any notify implied
that the event had been set (frankly a slightly insane assumption, but
whatever).
  • Loading branch information
einarmo authored Feb 20, 2024
1 parent 03a800d commit f9a079d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## 7.0.1

### Fixed

* Ensure that `CancellationToken.wait(timeout)` only waits for at most `timeout`, even if it is notified in that time.

## 7.0.0

### Changed
Expand Down
17 changes: 14 additions & 3 deletions cognite/extractorutils/threading.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import signal
from threading import Condition
from time import time
from typing import Any, Optional


Expand Down Expand Up @@ -59,11 +60,21 @@ def set(self) -> None:
self.cancel()

def wait(self, timeout: Optional[float] = None) -> bool:
endtime = None
if timeout is not None:
endtime = time() + timeout

while not self.is_cancelled:
with self._cv:
did_not_time_out = self._cv.wait(timeout)
if not did_not_time_out:
return False
if endtime is not None:
remaining_time = endtime - time()
if remaining_time <= 0.0:
return True
timed_out = not self._cv.wait(remaining_time)
if timed_out:
return False
else:
self._cv.wait()
return True

def create_child_token(self) -> "CancellationToken":
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cognite-extractor-utils"
version = "7.0.0"
version = "7.0.1"
description = "Utilities for easier development of extractors for CDF"
authors = ["Mathias Lohne <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit f9a079d

Please sign in to comment.