I am attempting to make a request to a third party API via a python script in a Firebase cloud function (2nd generation cloud functions python public preview):
def foo(req: https_fn.Request) -> https_fn.Response:
url = 'https://catfact.ninja/fact'
res = requests.get(url)
return json.dumps(res.json())
When I call this function, I get the following error:
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC() to debug.
I know this is related to the request because when I remove the request this error no longer occurs. I read online that this issue was a Mac related thing and I am indeed on a Mac. I also tried simply executing the above request in a separate python file, which worked fine. This leads me to believe this issue is related to something in Firebase Functions. Does anyone know what’s going on here?
2
Answers
Update for anyone who again may have this issue in the future - What I've found is that when deployed, the requests library will work with no issues. However on MacOS in Firebase Emulators it seems to cause some forking issues (don't know what this really even means). Using Python's native http.client library seems to fix this. Hope this saves some future person the 2 or 3 days of pain I went through :)
*Edit additional update:
After some more head scratching and Googling and consulting with GPT, I've learned that importing numpy in the root code of the function leads to issues I do not understand or care to further understand. What's important is that you import libraries such as numpy WITHIN your function defininitions, not in the root of the code:
Do this:
And not this:
Hope this is helpful to someone!
This error is caused by using fork mode for multiprocessing in MacOS.
Most likely the cause why the issue is resolved when using Python’s native http.client is because the library doesn’t rely on CoreFoundation. It can be concluded in this case that when the library is used, it implicitly uses that when making HTTP requests instead of using CoreFoundation functionality.
Another way to fix this kind of issue is to explicitly set the multiprocessing start method to spawn. Since using fork mode on MacOS is unsafe.