skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    from firebase_functions import https_fn
    from firebase_admin import initialize_app
    
    import requests
    import json
    
    initialize_app()
    
    
    @https_fn.on_request()
    def on_request_example(req: https_fn.Request) -> https_fn.Response:
        import numpy as np
        
        arrayFlt = [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
        stuff = np.array(arrayFlt)
        new_stuff = make_new_stuff(stuff)
        
        url = 'https://catfact.ninja/fact'
        res = requests.get(url)
        return json.dumps(res.json())
    

    And not this:

    from firebase_functions import https_fn
    from firebase_admin import initialize_app
    
    import requests
    import json
    import numpy as np
    
    initialize_app()
    
    
    @https_fn.on_request()
    def on_request_example(req: https_fn.Request) -> https_fn.Response:
        
        arrayFlt = [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
        stuff = np.array(arrayFlt)
        new_stuff = make_new_stuff(stuff)
        
        url = 'https://catfact.ninja/fact'
        res = requests.get(url)
        return json.dumps(res.json())
    

    Hope this is helpful to someone!


  2. This error is caused by using fork mode for multiprocessing in MacOS.

    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.

    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.

    multiprocessing.set_start_method('spawn')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search