fix: Add timeout, extract constant, harden avatar URL parsing, and use module-level logging#3481
Open
AliRana30 wants to merge 1 commit intofossasia:devfrom
Open
fix: Add timeout, extract constant, harden avatar URL parsing, and use module-level logging#3481AliRana30 wants to merge 1 commit intofossasia:devfrom
AliRana30 wants to merge 1 commit intofossasia:devfrom
Conversation
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideNarrowly scope exception handling in social avatar downloads and ticket secret parsing to avoid swallowing unexpected errors, add timeouts and logging for better diagnostics, and keep existing user-visible behavior consistent. Sequence diagram for social avatar download with timeout and scoped exception handlingsequenceDiagram
actor User
participant WebApp
participant SocialUtils
participant Requests
participant AvatarServer
participant StoredFile
participant Logging
User->>WebApp: Authenticate via social provider
WebApp->>SocialUtils: update_user_profile_from_social(user, info, backend)
SocialUtils->>Requests: get(avatar_url, timeout=10)
Requests->>AvatarServer: HTTP GET avatar_url
alt Request succeeds
AvatarServer-->>Requests: 200 OK + image bytes
Requests-->>SocialUtils: Response
SocialUtils->>Requests: raise_for_status()
SocialUtils->>StoredFile: StoredFile.objects.create(..., type=avatar, public=True)
StoredFile-->>SocialUtils: StoredFile instance
SocialUtils->>StoredFile: file.save(avatar.ext, content)
SocialUtils-->>WebApp: Updated user profile with avatar
else Request fails or times out
Requests-->>SocialUtils: Raise RequestException
SocialUtils->>Logging: logging.exception(Could not download avatar)
SocialUtils-->>WebApp: User profile unchanged (no avatar update)
end
Class diagram for social avatar update and Sig1TicketSecretGenerator parsing changesclassDiagram
class SocialUtils {
+update_user_profile_from_social(user, info, backend)
}
class Sig1TicketSecretGenerator {
-logger
+_parse(secret)
}
class RequestsLibrary {
+get(url, timeout)
}
class LoggingModule {
+exception(message)
+debug(message, error)
}
class StoredFile {
+objects
+file
+save(name, content)
}
class StoredFileManager {
+create(name, base_folder, type, public)
}
class Sig1TicketSecretExceptions {
binasciiError
structError
valueError
typeError
invalidSignature
indexError
decodeError
}
SocialUtils --> RequestsLibrary : uses
SocialUtils --> StoredFile : creates
SocialUtils --> LoggingModule : logs_download_failure
Sig1TicketSecretGenerator --> LoggingModule : logs_parse_failure
Sig1TicketSecretGenerator --> Sig1TicketSecretExceptions : catches
StoredFile o-- StoredFileManager : objects
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider extracting the
timeout=10value into a named constant or configuration setting so it’s easier to tune or reuse across other outbound requests.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the `timeout=10` value into a named constant or configuration setting so it’s easier to tune or reuse across other outbound requests.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the social-profile avatar fetch path to avoid hanging on slow/unresponsive avatar URLs by applying a request timeout and narrowing the exception handler to requests.RequestException.
Changes:
- Add a 10s timeout to the avatar
requests.get()call. - Replace a bare
except:withexcept requests.RequestExceptionfor avatar download failures.
Contributor
|
Hi @AliRana30, Please mention the issue you are fixing in the PR description. |
80649d6 to
3777b85
Compare
Author
|
hi @ViRUS-0-0, @Aqil-Ahmad! You can review the PR, I have added everything as requested. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This change improves reliability and observability of social avatar downloads by:
adjustment.
strips query/fragment, and falls back to a safe default when required).
Changes
EXTERNAL_REQUEST_TIMEOUT = 10constant for reusable outbound request timeout across social sync codeavatar_url.rsplit(".", 1)[-1]withurlparse(...).path+os.path.splitext(...)to safely handle query params (e.g.,jpg?e=12345) and ambiguous URLslogging.exception()to module-levellogger.exception()for consistent namespacing during social profile syncexcept:torequests.RequestExceptiononlyBefore
After
Closes : #3492