Skip to content

[BUG] Image-fetch timeout leaves header-reading workers and sockets alive #3854

Closed

Description

@Qubitium

Review follow-up to #3848, confirmed on main d831ba08b8b64c362f990d38fa7d6e779e417f10. Priority: P2.

load_image_from_url() now returns at its deadline, but the request itself is not cancelled while response headers are arriving. _get_response() starts a new daemon thread for every call; its cancellation event is checked only after requests.get() returns. While headers are still arriving, outcome contains no Response, so the timeout path cannot abort its socket. A server sending header bytes faster than the read timeout keeps the abandoned worker alive. Repeated failed URLs therefore accumulate threads and sockets even though every caller has already received ImageFetchError.

Source: worker and timeout handling.

This is distinct from #3832's caller blocking past the deadline, which #3848 fixes. It is a resource-lifecycle regression caused by moving that blocking operation onto an uninterruptible per-call thread.

Reproduction from the repository root (requires requests and Pillow; localhost HTTP, no network mocks or model download):

import importlib.util
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

spec = importlib.util.spec_from_file_location('image_utils', 'gptqmodel/utils/image.py')
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
stop = threading.Event()

class Handler(BaseHTTPRequestHandler):
    def log_message(self, *args):
        pass

    def do_GET(self):
        self.wfile.write(b'HTTP/1.1 200 OK\r\nX-Drip: ')
        self.wfile.flush()
        try:
            while not stop.wait(.02):
                self.wfile.write(b'x')
                self.wfile.flush()
        except OSError:
            pass

server = ThreadingHTTPServer(('127.0.0.1', 0), Handler)
server.daemon_threads = True
threading.Thread(target=server.serve_forever, daemon=True).start()
def workers():
    return sum(t.name == 'gptqmodel-image-fetch' for t in threading.enumerate())
try:
    for i in range(3):
        started = time.monotonic()
        try:
            m.load_image_from_url(
                f'http://127.0.0.1:{server.server_port}/',
                total_timeout=.1, timeout=(.1, .1), attempts=1,
            )
        except m.ImageFetchError:
            pass
        print(i + 1, round(time.monotonic() - started, 3), workers())
    time.sleep(1)
    print('one second later:', workers())
finally:
    stop.set()
    server.shutdown()
    server.server_close()

Observed on Python 3.14.7:

1 0.1 1
2 0.1 2
3 0.1 3
one second later: 3

Expected: deadline expiration terminates the underlying network operation and releases its worker/socket within a bounded cleanup interval. A cancellable transport with access to the socket during headers, or another mechanism that actually interrupts the operation, is needed; making the thread a daemon only changes interpreter-exit behavior. Add a regression asserting worker/socket cleanup after header-drip deadlines, not just elapsed time at the caller.

Validation: real localhost I/O against the complete unmodified image module, loaded directly to avoid unrelated model initialization. No GPU or full-model behavior is claimed. Existing image timeout regression cases pass because their assertions observe only the caller; their finite header drips also eventually release the worker. Searched existing issues for image/thread, image/leak, and header; found #3832 but no report of this residual worker/socket lifetime.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions