You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
credentails.json is used to authenticate to Google Cloud and have access to Firestore. For this example, we assume that Firestore is set up for the project and can be access by this service account key.
Dockerfile, main.py and requirements.txt are provided below.
Running the following curl command output the expected result:
$ curl http://localhost:8888
Count: 0.0
At this point the code works well, the issue appears when we restart the docker container, and we send multiple concurrent request to the endpoint using the hey HTTP load generator to simulate real traffic on our application:
From that point we are not getting any response back from application, and we receive Get "http://localhost:8888/": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
This error setup has been reproduced on Linux and Mac.
The error only seems to appear when we restart the container. It is also fix when we restart the container again. It is going in a loop of stuck, unstuck, stuck, unstuck.... We didn't manage to reproduce this bug by running this code outside of Docker.
Is there any undocumented caching or network protocol used by that tool that we should know of and that required some Docker config?
What we tested
Running the same code without the data = aggregate_query.count().get() line solve the timeout issue. We are no longer getting the data we need since we are not running it. By doing so, we isolated the issue to that line.
Adding the timeout parameter to the aggregate_query.count().get(timeout=2) does not do anything for us. This parameter doesn't seem to be working at all.
We tested this code on different network to exclude firewall rules that could block network calls.
Source code
main.py
"""BUGGED module."""fromdatetimeimportdatetime, timedeltafromtypingimportTupleimportflaskimportfunctions_frameworkfromflaskimportResponsefromgoogle.cloud.firestore_v1importQueryfromgoogle.cloud.firestore_v1.aggregationimportAggregationQueryfromgoogle.cloud.firestore_v1.base_queryimportFieldFilterfromgoogle.cloud.firestore_v1.clientimportClientasFirestoreClientFIRESTORE_CLIENT=FirestoreClient()
defcount_data_in_query_bugged(query: Query) ->int:
"""Count data in query."""print("Start counting data in query")
# Transform to aggregation query to countaggregate_query: AggregationQuery=AggregationQuery(query)
data=aggregate_query.count().get()
count=data[0][0].valueprint("end counting data in query")
returncount@functions_framework.httpdefentry_point(request: flask.Request) ->Tuple[Response|str, int]:
print("Request received")
start=datetime.now() -timedelta(days=1)
end=datetime.now()
query= (
FIRESTORE_CLIENT.collection("statistics")
.where(filter=FieldFilter("status", "==", "acceptable"))
.where(filter=FieldFilter("timestamp", ">=", start))
.where(filter=FieldFilter("timestamp", "<", end))
)
count=count_data_in_query_bugged(query)
print(count)
returnf"Count: {count}", 200
Dockerfile
FROM python:3.11
WORKDIR /app
COPY . .
# Install requirementsRUN pip install -r requirements.txt
ENV FUNCTION_TARGET="entry_point"ENV GOOGLE_APPLICATION_CREDENTIALS="/app/credentials.json"# Run cloud function locallyCMD functions-framework --target=$FUNCTION_TARGET --debug
Do you need to close the client or gracefully shutdown? What about a try-catch or increasing the client timeout secs?
I've tried to instantiate a new Firestore client at every function call to check if the globally available client was the issue. It didn't change the results of the experiment, we are still getting stuck
I've also recreated that experiment in Go using the function-framework-go library and I am not getting any errors/timeout so far. I can only affirm that the docker restart process to break the python code doesn't seem to work on the Go code. Switching to another programming language to overcome a critical issue of the package isn't a solution for us
Firestore AggregationQuery getting stuck
We are getting
Client.Timeout
error when running AggregationQuery to count the number of documents in a query inside a Docker container.How to reproduce
Use the code provided below with the following folder structure:
. ├── credentials.json ├── Dockerfile ├── main.py └── requirements.txt
credentails.json
is used to authenticate to Google Cloud and have access to Firestore. For this example, we assume that Firestore is set up for the project and can be access by this service account key.Dockerfile
,main.py
andrequirements.txt
are provided below.docker build -t bug . docker run -d -p 8888:8080 --name bug bug:latest
At this point the code works well, the issue appears when we restart the docker container, and we send multiple concurrent request to the endpoint using the hey HTTP load generator to simulate real traffic on our application:
From that point we are not getting any response back from application, and we receive
Get "http://localhost:8888/": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
This error setup has been reproduced on Linux and Mac.
The error only seems to appear when we restart the container. It is also fix when we restart the container again. It is going in a loop of
stuck, unstuck, stuck, unstuck...
. We didn't manage to reproduce this bug by running this code outside of Docker.Is there any undocumented caching or network protocol used by that tool that we should know of and that required some Docker config?
What we tested
data = aggregate_query.count().get()
line solve the timeout issue. We are no longer getting the data we need since we are not running it. By doing so, we isolated the issue to that line.timeout
parameter to theaggregate_query.count().get(timeout=2)
does not do anything for us. This parameter doesn't seem to be working at all.Source code
main.py
Dockerfile
requirements.txt
The text was updated successfully, but these errors were encountered: