From 70fd19ad753fa3fb26d27787fcd783e777acff18 Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Sat, 6 Apr 2024 06:51:02 -0300 Subject: [PATCH] fix(postgrest): race condition when executing request --- Sources/PostgREST/PostgrestBuilder.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/PostgREST/PostgrestBuilder.swift b/Sources/PostgREST/PostgrestBuilder.swift index 9bef83e9..ac15ec6e 100644 --- a/Sources/PostgREST/PostgrestBuilder.swift +++ b/Sources/PostgREST/PostgrestBuilder.swift @@ -76,7 +76,7 @@ public class PostgrestBuilder: @unchecked Sendable { options: FetchOptions, decode: (Data) throws -> T ) async throws -> PostgrestResponse { - mutableState.withValue { + let request = mutableState.withValue { $0.fetchOptions = options if $0.fetchOptions.head { @@ -103,9 +103,11 @@ public class PostgrestBuilder: @unchecked Sendable { $0.request.headers["Content-Profile"] = schema } } + + return $0.request } - let response = try await http.fetch(mutableState.value.request, baseURL: configuration.url) + let response = try await http.fetch(request, baseURL: configuration.url) guard 200 ..< 300 ~= response.statusCode else { let error = try configuration.decoder.decode(PostgrestError.self, from: response.data)