Skip to content

Commit

Permalink
Merge pull request #33 from zstyblik/pipe_open_timeout
Browse files Browse the repository at this point in the history
Set timeout on pipe open
  • Loading branch information
zstyblik authored Sep 3, 2024
2 parents 7851d77 + f5159d9 commit 1d475be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/config_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
CACHE_EXPIRATION = 86400 # seconds
DATA_SOURCE_EXPIRATION = 30 * 86400 # seconds
HTTP_TIMEOUT = 30 # seconds
PIPE_OPEN_TIMEOUT = 60 # seconds
PIPE_WRITE_TIMEOUT = 5 # seconds
11 changes: 7 additions & 4 deletions rss2irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def read_cache(logger: logging.Logger, cache_file: str) -> CachedData:

def signal_handler(signum, frame):
"""Handle SIGALRM signal."""
raise ValueError
raise TimeoutError


def scrub_items(logger: logging.Logger, cache: CachedData) -> None:
Expand Down Expand Up @@ -308,13 +308,16 @@ def write_data(
sleep: int = 2,
) -> None:
"""Write data into file."""
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(config_options.PIPE_OPEN_TIMEOUT)
with open(output, "wb") as fhandle:
signal.alarm(0)
for url in list(data.keys()):
message = format_message(url, data[url], handle)
try:
write_message(logger, fhandle, message)
time.sleep(sleep)
except ValueError:
except (TimeoutError, ValueError):
logger.debug("%s", traceback.format_exc())
logger.debug("Failed to write %s, %s", url, data[url])
data.pop(url)
Expand All @@ -325,10 +328,10 @@ def write_message(
) -> None:
"""Write message into file handle.
Sets up SIGALRM and raises `ValueError` if alarm is due.
Sets up SIGALRM and raises `TimeoutError` if alarm is due.
"""
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(5)
signal.alarm(config_options.PIPE_WRITE_TIMEOUT)
try:
fhandle_stat = os.fstat(fhandle.fileno())
is_fifo = stat.S_ISFIFO(fhandle_stat.st_mode)
Expand Down

0 comments on commit 1d475be

Please sign in to comment.