Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

[BUG] Logging not working from other threads #1158

Closed ManelBH closed 1 year ago

ManelBH commented 1 year ago

Investigative information

Python version: 3.9.10 Core Tools Version: 4.0.4915 Function Runtime Version: 4.14.0.19631

Repro steps

dispatch a function that uses logging to a background thread

Expected behavior

The logged text appears in the logs

Actual behavior

The text isn't logged

Example:

import logging
from concurrent.futures import ThreadPoolExecutor
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    with ThreadPoolExecutor() as pool:
        pool.submit(foo)

    return func.HttpResponse(
        "This HTTP triggered function executed successfully",
        status_code=200
    )

def foo():
    logging.info("Hello from foo")
    print("foo completed")

When I trigger this, In the console I can see "foo completed" but not "Hello from foo".

Contents of the requirements.txt file:

azure-functions==1.12.0

ManelBH commented 1 year ago

Related: https://github.com/Azure-Samples/fastapi-on-azure-functions/issues/11

bhagyshricompany commented 1 year ago

Thanks for reporting we will discuss this feature issue and update you on the inputs soon.

Qmatteo commented 1 year ago

@bhagyshricompany Any update on this?

YunchuWang commented 1 year ago

@Qmatteo we have looked and identified the cause, within python function worker there is a managed threadpoolexecutor which wraps cx's sync function by assigning function invocation id to thread_local. This does not apply to cx managed threadpoolexecutor and thread without function invocation id assigned will not be logged https://github.com/Azure/azure-functions-python-worker/blob/9a58193076e9b7b14990aa3fe09b5336b5c3db4c/azure_functions_worker/dispatcher.py#L713

we are working on the fix and will let you know

asalogni commented 1 year ago

@YunchuWang how long do you think it will take to fix the problem?

bhagyshricompany commented 1 year ago

@vrdmr please comment on this

EvanR-Dev commented 1 year ago

Hi, a potential fix is currently under review. Thank you for your patience, and we will keep you updated on this.

Qmatteo commented 1 year ago

@EvanR-Dev ok. Any ideas on the timeline? Coz I think that the PR must be reviewed, approved, completed and released. But right now we have some functions in production that we are not able to monitor since we don't see any logs

EvanR-Dev commented 1 year ago

I understand. It will be merged by the end of this week and part of the next release. The release will take time as we are doing hot fixes and could take by the end of March by the time it is finally out. We thank you for your patience once again.

GiuseppeChiesa-TomTom commented 1 year ago

has this fix been released?

gavin-aguiar commented 1 year ago

This fix will be released with runtime version 4.19. ETA mid-April

Marco-Santoni commented 1 year ago

Hi @gavin-aguiar ,

how can we track when the 4.19 is going live?

gavin-aguiar commented 1 year ago

@Marco-Santoni 4.19 is already deployed for dedicated plan in all regions. Consumption plan deployment has started and should be done in a week.

Eusinho1985 commented 3 months ago

Investigative information

Python version: 3.9.10 Core Tools Version: 4.0.4915 Function Runtime Version: 4.14.0.19631

Repro steps

dispatch a function that uses logging to a background thread

Expected behavior

The logged text appears in the logs

Actual behavior

The text isn't logged

Example:

import logging
from concurrent.futures import ThreadPoolExecutor
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    with ThreadPoolExecutor() as pool:
        pool.submit(foo)

    return func.HttpResponse(
        "This HTTP triggered function executed successfully",
        status_code=200
    )

def foo():
    logging.info("Hello from foo")
    print("foo completed")

When I trigger this, In the console I can see "foo completed" but not "Hello from foo".

Contents of the requirements.txt file:

azure-functions==1.12.0