Skip to content

Only 2 Secound Timout for Rest Calls #353

Open

Description

@paz0ok

Hi everyone,

I have an issue with the timeout for rest calls.
The issue I'm having is in Lib/_socket.py 858 (_handle_timeout).
The tool I'm using is running Python 2 and has a max int of 2147483647. This will give me only 2.14 seconds of timeout.
If I give the int() function more than the max int it will be automatically reduced.
Does anyone have a solution that can be implemented in the Jython repo?

Here a screenshot from the tool:
image

Thanks :)

Activity

  1. Andrysqui commented on Oct 11, 2024

    @Andrysqui

    I've been checking how to make the maximum number for the timeout larger.

    One way could be to in this line of the function:

    timeout_in_ns = int(self.timeout * _TO_NANOSECONDS)

    Use long instead of int so that the value for the timeout isn't reduced:

    timeout_in_ns = long(self.timeout * _TO_NANOSECONDS)

    So now the function would look like:

    def _handle_timeout(self, waiter, reason):
        timeout_in_ns = long(self.timeout * _TO_NANOSECONDS)  # Switch to 'long' for larger timeouts
        log.debug("Waiting for up to %.2fs for %s", self.timeout, reason, extra={"sock": self})
        
        started = time.time()
        result = waiter(timeout_in_ns, TimeUnit.NANOSECONDS)
        
        log.debug("Completed in %.2fs", time.time() - started, extra={"sock": self})
        
        if not result:
            if self.timeout == 0:
                raise error(errno.ETIMEDOUT, "Connection timed out")
            else:
                raise timeout(errno.ETIMEDOUT, "timed out")
        
        return result

    This would increase the limit for the timeout to pretty much infinity as much as your device has enough memory, or the number isn't big enough to cause any issues with the library or Jython in general.

    Maybe it could be a good idea to add something like a limit to make it more difficult for something to end up breaking:

    def _handle_timeout(self, waiter, reason):
        timeout_in_ns = long(self.timeout * _TO_NANOSECONDS)  # Use 'long' for larger timeouts
        max_timeout_in_ns = long(2.592e+15)  # Maximum nanoseconds, equal to the number of nanoseconds in a 30-day month
    
        if timeout_in_ns > max_timeout_in_ns:
            raise ValueError("Timeout exceeds 30-day month limit")
    
        log.debug("Waiting for up to %.2fs for %s", self.timeout, reason, extra={"sock": self})
        
        started = time.time()
        result = waiter(timeout_in_ns, TimeUnit.NANOSECONDS)
        
        log.debug("Completed in %.2fs", time.time() - started, extra={"sock": self})
        
        if not result:
            if self.timeout == 0:
                raise error(errno.ETIMEDOUT, "Connection timed out")
            else:
                raise timeout(errno.ETIMEDOUT, "timed out")
        
        return result

    I still need to check that using long here doesn't break anything, in case it did another idea that I have is to use float instead of int to manage the timeout. Which could work better than using long but it would probably take more time to make compatible with everything.

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