{ "cells": [ { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "slide" } }, "source": [ "# Timeout\n", "\n", "The code in this notebook helps in interrupting execution after a given time." ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "source": [ "**Prerequisites**\n", "\n", "* This notebook needs some understanding on advanced concepts in Python, notably \n", " * classes\n", " * the Python `with` statement\n", " * the Python `signal` functions\n", " * measuring time" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "## Synopsis\n", "\n", "\n", "To [use the code provided in this chapter](Importing.ipynb), write\n", "\n", "```python\n", ">>> from fuzzingbook.Timeout import \n", "```\n", "\n", "and then make use of the following features.\n", "\n", "\n", "The `Timeout` class throws a `TimeoutError` exception after a given timeout has expired.\n", "Its typical usage is in conjunction with a `with` clause:\n", "\n", "```python\n", ">>> try:\n", ">>> with Timeout(0.2):\n", ">>> some_long_running_function()\n", ">>> print(\"complete!\")\n", ">>> except TimeoutError:\n", ">>> print(\"Timeout!\")\n", "Timeout!\n", "\n", "```\n", "Note: On Unix/Linux systems, the `Timeout` class uses [`SIGALRM` signals](https://docs.python.org/3.10/library/signal.html) (interrupts) to implement timeouts; this has no effect on performance of the tracked code. On other systems (notably Windows), `Timeout` uses the [`sys.settrace()`](https://docs.python.org/3.10/library/sys.html?highlight=settrace#sys.settrace) function to check the timer after each line of code, which affects performance of the tracked code.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "slide" } }, "source": [ "## Measuring Time\n", "\n", "The class `Timeout` allows interrupting some code execution after a given time interval." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "button": false, "execution": { "iopub.execute_input": "2024-04-27T13:59:30.236213Z", "iopub.status.busy": "2024-04-27T13:59:30.235893Z", "iopub.status.idle": "2024-04-27T13:59:30.281202Z", "shell.execute_reply": "2024-04-27T13:59:30.280775Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import bookutils.setup" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "button": false, "execution": { "iopub.execute_input": "2024-04-27T13:59:30.284254Z", "iopub.status.busy": "2024-04-27T13:59:30.284020Z", "iopub.status.idle": "2024-04-27T13:59:30.286140Z", "shell.execute_reply": "2024-04-27T13:59:30.285772Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import time" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.288458Z", "iopub.status.busy": "2024-04-27T13:59:30.288319Z", "iopub.status.idle": "2024-04-27T13:59:30.290408Z", "shell.execute_reply": "2024-04-27T13:59:30.289977Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# ignore\n", "from typing import Type, Any, Callable, Union, Optional" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.292767Z", "iopub.status.busy": "2024-04-27T13:59:30.292597Z", "iopub.status.idle": "2024-04-27T13:59:30.294577Z", "shell.execute_reply": "2024-04-27T13:59:30.294270Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from types import FrameType, TracebackType" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Variant 1: Unix (using signals, efficient)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.296786Z", "iopub.status.busy": "2024-04-27T13:59:30.296638Z", "iopub.status.idle": "2024-04-27T13:59:30.298760Z", "shell.execute_reply": "2024-04-27T13:59:30.298363Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import signal" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "button": false, "execution": { "iopub.execute_input": "2024-04-27T13:59:30.300616Z", "iopub.status.busy": "2024-04-27T13:59:30.300459Z", "iopub.status.idle": "2024-04-27T13:59:30.304111Z", "shell.execute_reply": "2024-04-27T13:59:30.303655Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "class SignalTimeout:\n", " \"\"\"Execute a code block raising a timeout.\"\"\"\n", "\n", " def __init__(self, timeout: Union[int, float]) -> None:\n", " \"\"\"\n", " Constructor. Interrupt execution after `timeout` seconds.\n", " \"\"\"\n", " self.timeout = timeout\n", " self.old_handler: Any = signal.SIG_DFL\n", " self.old_timeout = 0.0\n", "\n", " def __enter__(self) -> Any:\n", " \"\"\"Begin of `with` block\"\"\"\n", " # Register timeout() as handler for signal 'SIGALRM'\"\n", " self.old_handler = signal.signal(signal.SIGALRM, self.timeout_handler)\n", " self.old_timeout, _ = signal.setitimer(signal.ITIMER_REAL, self.timeout)\n", " return self\n", "\n", " def __exit__(self, exc_type: Type, exc_value: BaseException,\n", " tb: TracebackType) -> None:\n", " \"\"\"End of `with` block\"\"\"\n", " self.cancel()\n", " return # re-raise exception, if any\n", "\n", " def cancel(self) -> None:\n", " \"\"\"Cancel timeout\"\"\"\n", " signal.signal(signal.SIGALRM, self.old_handler)\n", " signal.setitimer(signal.ITIMER_REAL, self.old_timeout)\n", "\n", " def timeout_handler(self, signum: int, frame: Optional[FrameType]) -> None:\n", " \"\"\"Handle timeout (SIGALRM) signal\"\"\"\n", " raise TimeoutError()" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "source": [ "Here's an example:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.305840Z", "iopub.status.busy": "2024-04-27T13:59:30.305746Z", "iopub.status.idle": "2024-04-27T13:59:30.307954Z", "shell.execute_reply": "2024-04-27T13:59:30.307542Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def some_long_running_function() -> None:\n", " i = 10000000\n", " while i > 0:\n", " i -= 1" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.309813Z", "iopub.status.busy": "2024-04-27T13:59:30.309687Z", "iopub.status.idle": "2024-04-27T13:59:30.514682Z", "shell.execute_reply": "2024-04-27T13:59:30.514153Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Timeout!\n" ] } ], "source": [ "try:\n", " with SignalTimeout(0.2):\n", " some_long_running_function()\n", " print(\"Complete!\")\n", "except TimeoutError:\n", " print(\"Timeout!\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Variant 2: Generic / Windows (using trace, not very efficient)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.535536Z", "iopub.status.busy": "2024-04-27T13:59:30.535388Z", "iopub.status.idle": "2024-04-27T13:59:30.537420Z", "shell.execute_reply": "2024-04-27T13:59:30.537128Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.539284Z", "iopub.status.busy": "2024-04-27T13:59:30.539122Z", "iopub.status.idle": "2024-04-27T13:59:30.542883Z", "shell.execute_reply": "2024-04-27T13:59:30.542468Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "class GenericTimeout:\n", " \"\"\"Execute a code block raising a timeout.\"\"\"\n", "\n", " def __init__(self, timeout: Union[int, float]) -> None:\n", " \"\"\"\n", " Constructor. Interrupt execution after `timeout` seconds.\n", " \"\"\"\n", "\n", " self.seconds_before_timeout = timeout\n", " self.original_trace_function: Optional[Callable] = None\n", " self.end_time: Optional[float] = None\n", "\n", " def check_time(self, frame: FrameType, event: str, arg: Any) -> Callable:\n", " \"\"\"Tracing function\"\"\"\n", " if self.original_trace_function is not None:\n", " self.original_trace_function(frame, event, arg)\n", "\n", " current_time = time.time()\n", " if self.end_time and current_time >= self.end_time:\n", " raise TimeoutError\n", "\n", " return self.check_time\n", "\n", " def __enter__(self) -> Any:\n", " \"\"\"Begin of `with` block\"\"\"\n", " start_time = time.time()\n", " self.end_time = start_time + self.seconds_before_timeout\n", "\n", " self.original_trace_function = sys.gettrace()\n", " sys.settrace(self.check_time)\n", " return self\n", "\n", " def __exit__(self, exc_type: type, \n", " exc_value: BaseException, tb: TracebackType) -> Optional[bool]:\n", " \"\"\"End of `with` block\"\"\"\n", " self.cancel()\n", " return None # re-raise exception, if any\n", "\n", " def cancel(self) -> None:\n", " \"\"\"Cancel timeout\"\"\"\n", " sys.settrace(self.original_trace_function)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Again, our example:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.544622Z", "iopub.status.busy": "2024-04-27T13:59:30.544528Z", "iopub.status.idle": "2024-04-27T13:59:30.747070Z", "shell.execute_reply": "2024-04-27T13:59:30.746559Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Timeout!\n" ] } ], "source": [ "try:\n", " with GenericTimeout(0.2):\n", " some_long_running_function()\n", " print(\"Complete!\")\n", "except TimeoutError:\n", " print(\"Timeout!\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Choosing the right variant" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.749466Z", "iopub.status.busy": "2024-04-27T13:59:30.749303Z", "iopub.status.idle": "2024-04-27T13:59:30.751509Z", "shell.execute_reply": "2024-04-27T13:59:30.751101Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "Timeout: Type[SignalTimeout] = SignalTimeout if hasattr(signal, 'SIGALRM') else GenericTimeout # type: ignore" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Synopsis\n", "\n", "The `Timeout` class throws a `TimeoutError` exception after a given timeout has expired.\n", "Its typical usage is in conjunction with a `with` clause:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-04-27T13:59:30.754130Z", "iopub.status.busy": "2024-04-27T13:59:30.753959Z", "iopub.status.idle": "2024-04-27T13:59:30.961244Z", "shell.execute_reply": "2024-04-27T13:59:30.960625Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Timeout!\n" ] } ], "source": [ "try:\n", " with Timeout(0.2):\n", " some_long_running_function()\n", " print(\"complete!\")\n", "except TimeoutError:\n", " print(\"Timeout!\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Note: On Unix/Linux systems, the `Timeout` class uses [`SIGALRM` signals](https://docs.python.org/3.10/library/signal.html) (interrupts) to implement timeouts; this has no effect on performance of the tracked code. On other systems (notably Windows), `Timeout` uses the [`sys.settrace()`](https://docs.python.org/3.10/library/sys.html?highlight=settrace#sys.settrace) function to check the timer after each line of code, which affects performance of the tracked code." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Create a `Timeout` variant that works efficiently on Windows. Note that how to do this a long debated issue in programming forums." ] } ], "metadata": { "ipub": { "bibliography": "fuzzingbook.bib", "toc": true }, "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true }, "toc-autonumbering": false, "vscode": { "interpreter": { "hash": "0af4f07dd039d1b4e562c7a7d0340393b1c66f50605ac6af30beb81aa23b7ef5" } } }, "nbformat": 4, "nbformat_minor": 4 }