Add timeout to contributor avatar fetch#1057
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
| import requests | ||
| from PIL import Image | ||
|
|
||
| REQUEST_TIMEOUT_SECONDS = 10 |
There was a problem hiding this comment.
Self-review: Keeping the timeout as a named constant makes it easy to tune later and avoids leaving a maintenance script blocked indefinitely on one slow avatar response.
There was a problem hiding this comment.
Code Review
This pull request introduces a timeout for network requests when fetching contributor avatars to prevent the script from hanging indefinitely. The reviewer suggested enhancing error handling by adding response.raise_for_status() to ensure the script fails explicitly on HTTP errors.
| response = requests.get( | ||
| contributor["avatar_url"], timeout=REQUEST_TIMEOUT_SECONDS | ||
| ) |
There was a problem hiding this comment.
While adding a timeout is a good practice to prevent the script from hanging, it's also important to ensure the request was successful before processing the response. Adding response.raise_for_status() will cause the script to fail with a clear HTTP error if the avatar download fails (e.g., 404 or 500), which is consistent with the 'fail fast' goal of this PR.
| response = requests.get( | |
| contributor["avatar_url"], timeout=REQUEST_TIMEOUT_SECONDS | |
| ) | |
| response = requests.get( | |
| contributor["avatar_url"], timeout=REQUEST_TIMEOUT_SECONDS | |
| ) | |
| response.raise_for_status() |
There was a problem hiding this comment.
Thanks, accepted in 1e1baef by adding response.raise_for_status() after the avatar request.
Summary
Why
The helper currently waits indefinitely if an avatar request stalls. A small timeout makes the maintenance script fail fast instead of hanging forever on a single contributor image.
Testing