Tear down ``scan_id``'s container and drop its cache entry. Best-effort: any error during ``client.delete`` is logged and swallowed. We never want a cleanup failure to prevent the next scan from starting; the worst case is a stranded container that Docker's normal reaping will catch
(scan_id: str)
| 104 | |
| 105 | |
| 106 | async def cleanup(scan_id: str) -> None: |
| 107 | """Tear down ``scan_id``'s container and drop its cache entry. |
| 108 | |
| 109 | Best-effort: any error during ``client.delete`` is logged and |
| 110 | swallowed. We never want a cleanup failure to prevent the next |
| 111 | scan from starting; the worst case is a stranded container that |
| 112 | Docker's normal reaping will catch on next ``docker prune``. |
| 113 | """ |
| 114 | bundle = _SESSION_CACHE.pop(scan_id, None) |
| 115 | if bundle is None: |
| 116 | logger.debug("cleanup(%s): no cached session", scan_id) |
| 117 | return |
| 118 | |
| 119 | caido_client = bundle.get("caido_client") |
| 120 | if caido_client is not None: |
| 121 | try: |
| 122 | await caido_client.aclose() |
| 123 | except Exception: # noqa: BLE001 |
| 124 | logger.debug("cleanup(%s): caido_client.aclose() raised", scan_id, exc_info=True) |
| 125 | |
| 126 | try: |
| 127 | await bundle["client"].delete(bundle["session"]) |
| 128 | logger.info("Cleaned up sandbox session for scan %s", scan_id) |
| 129 | except Exception: |
| 130 | logger.exception( |
| 131 | "cleanup(%s): client.delete raised; container may need manual reaping", |
| 132 | scan_id, |
| 133 | ) |