Fix: Add timeout to external IP address retrieval to prevent indefinite blocking #65
+1
−1
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.
Problem
The
SmartConnectclass contains module-level code that executes on import, making HTTP requests toapi.ipify.orgwithout a timeout. This causes several critical issues:api.ipify.orgexperiences outages (which can last hours), theget()call blocks indefinitely waiting for a response__init__), it executes whenever the module is imported, even if the class is never instantiatedapi.ipify.orgoutages, regardless of whetherSmartConnectis actually usedRoot Cause
The
requests.get()call lacks atimeoutparameter, causing it to wait indefinitely for a response when the external service is unavailable.Solution
Add a 5-second timeout to the
get()request to ensure the import process completes within a reasonable timeframe, allowing dependent applications to continue functioning even when the external IP service is down.Changes
timeout=5parameter to therequests.get()callCode Changes
Testing
SmartConnectno longer hang indefinitely during service outagesBenefits
Risk Assessment
Low Risk: This is a defensive change that only affects error scenarios. Normal operation is unchanged, and the existing exception handling ensures graceful degradation when the timeout is reached.
Note: While this PR addresses the immediate blocking issue, consider refactoring the module-level execution in a future release to move IP retrieval into the class initialization or a lazy-loading pattern to further improve import performance and reliability.