Skip to content

Client.close() does not close transports if unused #1317

Closed

Description

@johtso

Currently, Client.close() closes its transports if and only if the client is deemed to be "open" (not self._is_closed).

When the client is instantiated, it is not considered open, rather only when something is sent, or when it is __enter__ed.

Proxies and transports are initialized when the client is instantiated.

This means that if a client is not used as a context manager, and no requests are sent with it, proxies and transport are not closed when Client.close() is called.

This obviously doesn't cause any problems with the standard transports, but a custom transport may be in use that sets up some other resources.

Could we not just .close() the transports regardless of whether the client has been used?

Activity

  1. changed the title [-]Client.close() does not close transports if unused[/-] [+]`Client.close()` does not close transports if unused[/+] on Sep 24, 2020
  2. changed the title [-]`Client.close()` does not close transports if unused[/-] [+]Client.close() does not close transports if unused[/+] on Sep 24, 2020
  3. florimondmanca commented on Sep 24, 2020

    @florimondmanca
    Contributor

    Well, looks related to #1197 which I personally hadn't gone through and reviewed properly at the time…

    The intiialization here in particular seems dubious: https://github.com/encode/httpx/pull/1197/files#diff-eff812167840d2389ea5f0d809b30c32R83

    Instead of initializing it to False, we're setting it to True and then turning the flag down when making our first request... https://github.com/encode/httpx/pull/1197/files#diff-eff812167840d2389ea5f0d809b30c32R708

    This seems to suppose that the client can be "reopened" when it's been closed, which I don't think is the way we want to think about a client. When it's been closed, then that's it — no more reusing it. Clients are not "re-entrant".

    So in my mind we should init self._is_closed = False, drop any switch of that flag in .request() or __enter__(), and we should be good wrt the if self.is_closed check in __exit__(). Thoughts?

  4. florimondmanca commented on Sep 24, 2020

    @florimondmanca
    Contributor

    If you're up for it, seems like a quick fix we could sneak into 0.15.3 (#1318). :-)

  5. johtso commented on Sep 24, 2020

    @johtso
    ContributorAuthor

    @florimondmanca will take a bash!

  6. lovelydinosaur commented on Sep 24, 2020

    @lovelydinosaur
    Member

    @johtso Ah crossed wires - I'd already started taking a go at this too. #1320

    Clients are not "re-entrant".

    We'd not been explicit about that before, but it's probably the sensible thing to do, yup.

    I'd suggest we probably want the next proper version bump to hard-enforce that, raising an error if you attempt to send a request on a closed client.

  7. lovelydinosaur commented on Sep 24, 2020

    @lovelydinosaur
    Member

    FWIW I'd consider this more UX than bug. It's also perfectly sensible to treat a client as "closed" until either __enter__ is called, or a request is made.

    If you do want clients that can be reopened, then that makes sense, which I think was partly what pushed us in this direction originally, but yeah the more constrained variation is more in line with requests, and probably more in line with user expectations.

  8. added
    user-experienceEnsuring that users have a good experience using the library
    and removed
    bugSomething isn't working
    on Sep 25, 2020
  9. lovelydinosaur commented on Sep 25, 2020

    @lovelydinosaur
    Member

    Re-labeled this, since it's fuzzy.

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

    user-experienceEnsuring that users have a good experience using the library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions