Azure-functions-host: Azure Function runtime v3 returns status "500 OK" when ran.

Created on 14 Dec 2019  路  11Comments  路  Source: Azure/azure-functions-host

After the Http-triggered function runs through to completion successfully (Executed 'FunctionName' (Succeeded, Id=...), the Http response status code states "500 OK" and is not considered an "ok" status code.

image

Investigative information

  • Timestamp: 2019-12-14T11:43:43.541
  • Function App version: 3.0:
  • Function App name: contract-management-jobs
  • Function name: DeployVmsSchema
  • Invocation ID: 86613b19-68d1-4681-bd67-d1b33bbc1f47
  • Region: West Europe

Repro steps

  1. Open the network tab of the browser.
  2. Run the http-triggered azure function
  3. Observe the server response status code on the function

Expected behavior

Expected status code: 200 OK

Actual behavior

Status core is 500 OK, not considered sucessful.
Trace logs:
2019-12-14T11:43:59.694 [Information] Executed 'DeployVmsSchema' (Succeeded, Id=86613b19-68d1-4681-bd67-d1b33bbc1f47)

Known workarounds

Check for HTTP status text instead of status code to determine request success.

Related information

  • Programming language used: c#

Source

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.AuthLevelValue, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{

        [FunctionName("DeployVmsSchema")]
        public async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
            HttpRequestMessage req, ILogger log)
        {
            // ...
            try
            {
                // ...
            }
            catch (Exception e)
            {
                log.LogError($"An error occured during model deployment. {e.Message}", e);
                return req.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message);
            }

            return req.CreateResponse(HttpStatusCode.OK, "Schema deployed successfully.");
        }


-->

3.x

Most helpful comment

I figured out the answer to my question:

return await new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new StringContent(JsonConvert.SerializeObject(responseObj), Encoding.UTF8, "application/json"),
};

All 11 comments

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

Moving to active questions to setup and repro and investigate.

I had similar problem, in my case there is also header Content-Length=0, what is lie - function should return non-null response.
For me workaround was using IActionResult (e.g. OkObjectResult) instead of HttpResponseMessage.
microsoft.net.sdk.functions v 3.0.2, runtime azure 3.0.13107

I too am using Request.CreateResponse and getting 500 OK with the error "Microsoft.AspNetCore.Server.Kestrel.Core: Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead." I believe this is due to the breaking change in Asp.Net Core 3.0 that disabled synchronous I/O by default.

https://docs.microsoft.com/en-us/dotnet/core/compatibility/aspnetcore#http-synchronous-io-disabled-in-all-servers

CreateResponse must use synchronous IO.

@asalvo yes that is the issue. I had the same issue using GraphQL .NET server 3.4.0
Setting the environment variable FUNCTIONS_V2_COMPATIBILITY_MODE to true worked for me.

https://stackoverflow.com/questions/59379971/enable-synchronous-io-in-azure-function
https://github.com/Azure/azure-functions-durable-extension/issues/1039

Moving this to Triaged as this issue is specific to V3

I'm getting "500 Bad Request" for this code:

return request.CreateErrorResponse(HttpStatusCode.BadRequest, new HttpError("Invalid signature"));

I'd like to remove the FUNCTIONS_V2_COMPATIBILITY_MODE environment variable from my project - is there a more appropriate way to create an HTTP response that doesn't use synchronous IO?

I figured out the answer to my question:

return await new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new StringContent(JsonConvert.SerializeObject(responseObj), Encoding.UTF8, "application/json"),
};

I would not recommend enabling FUNCTIONS_V2_COMPATIBILITY_MODE because Microsoft will disable V2 functions soon. Substituting CreateResponse to the code below worked:

return new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new StringContent(JsonConvert.SerializeObject(responseObj), Encoding.UTF8, "application/json"),
};

cc @brettsam

Thanks @newOnahtaN for the code snippet to use in V3.

Closing this issue. As @gabrielbaptista - we recommend updating code and moving to V3. Until then use FUNCTIONS_V2_COMPATIBILITY_MODE

Was this page helpful?
0 / 5 - 0 ratings