fix(storage): resolve transport and retry issues#8235
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors request handling and retry logic by transitioning from the common Service class to a specialized StorageTransport. Key changes include the introduction of granular idempotency logic for HTTP methods, improved encryption header management, and a custom parameter serializer. Review feedback identifies a high-severity race condition involving shared interceptors on the Gaxios instance and inconsistent return types in the response handler. Additionally, improvements were suggested regarding the efficiency of JSON parsing within retry loops and the safety of non-null assertions in encryption headers.
| this.gaxiosInstance.interceptors.request.clear(); | ||
| if (reqOpts.interceptors) { | ||
| this.gaxiosInstance.interceptors.request.clear(); | ||
| for (const inter of reqOpts.interceptors) { | ||
| this.gaxiosInstance.interceptors.request.add(inter); | ||
| } | ||
| } |
There was a problem hiding this comment.
Clearing and adding interceptors on a shared this.gaxiosInstance before each asynchronous request creates a race condition. If multiple makeRequest calls are in flight, one call may clear the interceptors intended for another. Additionally, verify if this.authClient.request actually uses this.gaxiosInstance; if it doesn't, these interceptors are ineffective.
There was a problem hiding this comment.
required for retry operations fix in conformance Test PR
There was a problem hiding this comment.
What is the retry operations fix and why do we need to make this change in the current PR ? Same for the other comments below.
There was a problem hiding this comment.
To support custom headers required by conformance tests without creating race conditions where concurrent async requests overwrite each other's configuration on a shared instance, we modified makeRequest to isolate custom interceptors using a fresh, request-scoped Gaxios instance, while explicitly stripping the adapter property during cloning to prevent infinite recursion and heap out-of-memory crashes between GoogleAuth and Gaxios.
| const responseHandler = (resp: GaxiosResponse<T>) => { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const data = resp.data as any; | ||
| if (data !== null && typeof data === 'object') { | ||
| data.headers = resp.headers; | ||
| data.status = resp.status; | ||
| return data; | ||
| } | ||
| return resp; | ||
| }; |
There was a problem hiding this comment.
The responseHandler returns inconsistent types. If resp.data is an object, it returns the data with injected headers and status properties. Otherwise, it returns the full GaxiosResponse object. This inconsistency will likely cause runtime errors for consumers expecting a uniform return structure. Furthermore, injecting properties into resp.data is risky if the data is a Buffer or Stream.
There was a problem hiding this comment.
required for retry operations
There was a problem hiding this comment.
Please elaborate as stated in the above comment.
There was a problem hiding this comment.
To preserve backward compatibility for legacy SDK methods that expect resource metadata objects to carry .headers and .status properties without introducing runtime errors or mutating binary data, we added a strict validation check to ensure these properties are strictly injected into plain JSON response objects and never onto Buffer or Stream payloads.
| // Optimized Precondition Check | ||
| let bodyEtag = false; | ||
| try { | ||
| const parsedBody = typeof data === 'string' ? JSON.parse(data) : data; |
There was a problem hiding this comment.
Parsing the request body using JSON.parse inside the retry logic is inefficient for large payloads and will be executed on every retryable error check. Consider checking for preconditions earlier or in a way that avoids repeated parsing of the body. Additionally, this logic may fail or behave unexpectedly if data is a Buffer or Stream.
There was a problem hiding this comment.
required for retry operations fix in conformance Test PR
There was a problem hiding this comment.
Please elaborate as stated in the above comment.
There was a problem hiding this comment.
To eliminate the severe performance degradation and stream-breaking risks of repeatedly running JSON.parse inside the retry loop to detect preconditions like an etag, we now pre-calculate this presence once during the request preparation phase in StorageTransport.makeRequest and cache it as a simple boolean flag on the request configuration for the retry evaluator to read instantly.
| return { | ||
| 'x-goog-encryption-algorithm': 'AES256', | ||
| 'x-goog-encryption-key': this.encryptionKey.toString('base64'), | ||
| 'x-goog-encryption-key-sha256': this.encryptionKeyHash!, |
There was a problem hiding this comment.
The non-null assertion this.encryptionKeyHash! is unsafe. If encryptionKeyHash is missing, the header will be set to undefined. It's better to provide a fallback value.
| 'x-goog-encryption-key-sha256': this.encryptionKeyHash!, | |
| 'x-goog-encryption-key-sha256': this.encryptionKeyHash || '', |
There was a problem hiding this comment.
Resolved without response. Reopening again.
There was a problem hiding this comment.
Done. Replaced the non-null assertion with an empty string fallback (|| '') to safely handle cases where encryptionKeyHash is missing and prevent undefined headers.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…sport (googleapis#8283) - Remove Service.ts and common.ts files from handwritten/storage - Migrate remaining functionality to StorageTransport - chore(ci): upgrade conformance tests to Node 18
011f5b5 to
1ae557f
Compare
…transport-retry-fix
…sport (googleapis#8283) - Remove Service.ts and common.ts files from handwritten/storage - Migrate remaining functionality to StorageTransport - chore(ci): upgrade conformance tests to Node 18
1ae557f to
cc411a0
Compare
…transport-retry-fix
…sport (googleapis#8283) - Remove Service.ts and common.ts files from handwritten/storage - Migrate remaining functionality to StorageTransport - chore(ci): upgrade conformance tests to Node 18
cc411a0 to
9010041
Compare
…transport-retry-fix
9010041 to
0e8f067
Compare
…transport-retry-fix
…request interception and test bench integration
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #<issue_number_goes_here> 🦕