Skip to content

Refactor Scan.run

Wout De Nolf requested to merge refactor_scan_run into master

Use contextlib.ExitStack in Scan.run to clarify the code by putting related setup/teardown blocks together.

The previous logic had some issues when CTRL-C during the setup or teardown of Scan.run:

  • _set_state not always properly done (USER_ABORTED, KILLED, DONE)
  • KeyboardError -> ScanAbort not always done

The Scan.run is basically a nesting of a structure that looks like this:

with capture_exceptions(raise_index=0) as capture:
    with capture():
        <setup1>
        try:
            with capture():
                ...
        finally:
            with capture():
                <cleanup1a>
            with capture():
                <cleanup1b>

The capture context captures BaseException and adds it to the list of exceptions inside the locals of the capture_exceptions context which will re-raise the FIRST exception (if any) upon exit.

Expanded for only two contexts it looks like this:

with capture_exceptions(raise_index=0) as capture:
    with capture():
        <setup1>
        try:
            with capture():
                <setup2>
                try:
                    with capture():
                        ...
                finally:
                    with capture():
                        <cleanup2a>
                    with capture():
                        <cleanup2b>
        finally:
            with capture():
                <cleanup1a>
            with capture():
                <cleanup1b>
Edited by Wout De Nolf

Merge request reports