Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 500s from OPTIONS requests #4339

Open
premun opened this issue Jan 17, 2025 · 0 comments
Open

Fix 500s from OPTIONS requests #4339

premun opened this issue Jan 17, 2025 · 0 comments

Comments

@premun
Copy link
Member

premun commented Jan 17, 2025

Context

Whenever PCS receives an OPTIONS request (e.g. OPTIONS https://maestro.dot.net/index.html, it can be easily reproduced locally too), the SPA middleware blows up with an exception:

{
    "severityLevel": "Error",
    "outerId": "0",
    "message": "The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request.\nYour application is running in Production mode, so make sure it has been published, or that you have built your SPA manually. Alternatively you may wish to switch to the Development environment.\n",
    "type": "System.InvalidOperationException",
    "id": "51846141",
    "parsedStack": [
        {
            "assembly": "Microsoft.AspNetCore.SpaServices.Extensions, Version=8.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60",
            "method": "Microsoft.AspNetCore.SpaServices.SpaDefaultPageMiddleware+<>c__DisplayClass0_0.<Attach>b__1",
            "level": 0
        },
        {
            "assembly": "Microsoft.AspNetCore.StaticFiles, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60",
            "method": "Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke",
            "level": 1
        },
        {
            "assembly": "Microsoft.AspNetCore.SpaServices.Extensions, Version=8.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60",
            "method": "Microsoft.AspNetCore.SpaServices.SpaDefaultPageMiddleware+<>c__DisplayClass0_0.<Attach>b__0",
            "level": 2
        },
        {
            "assembly": "Microsoft.AspNetCore.Diagnostics, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60",
            "method": "Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware+<Invoke>d__3.MoveNext",
            "level": 3
        },
        {
            "assembly": "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
            "method": "System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw",
            "level": 4
        },
        {
            "assembly": "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
            "method": "System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess",
            "level": 5
        },
        {
            "assembly": "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
            "method": "System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification",
            "level": 6
        },
        {
            "assembly": "Microsoft.AspNetCore.Authorization.Policy, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60",
            "method": "Microsoft.AspNetCore.Authorization.AuthorizationMiddleware+<Invoke>d__11.MoveNext",
            "level": 7
        },
        {
            "assembly": "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
            "method": "System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw",
            "level": 8
        },
        {
            "assembly": "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
            "method": "System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess",
            "level": 9
        },
        {
            "assembly": "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
            "method": "System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification",
            "level": 10
        },
        {
            "assembly": "Microsoft.AspNetCore.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60",
            "method": "Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+<Invoke>d__6.MoveNext",
            "level": 11
        },
        {
            "assembly": "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
            "method": "System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw",
            "level": 12
        },
        {
            "assembly": "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
            "method": "System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess",
            "level": 13
        },
        {
            "assembly": "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
            "method": "System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification",
            "level": 14
        },
        {
            "assembly": "Microsoft.AspNetCore.Server.Kestrel.Core, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60",
            "method": "Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequests>d__238`1.MoveNext",
            "level": 15
        }
    ]
}

These requests are sent by most browsers to figure out how to download the web pages the best so exceptions happen each time someone visits BarViz.
These exceptions are then polluting application insights and it's not possible for us to configure alerting properly.

The problem is probably in the middleware (dotnet/aspnetcore#5223 (comment)) and goes like this:

  • The request for index.html OPTIONS hits the SPA middleware
  • The SPA middleware checks and sees the index.html page and delegates the request to the local static file middleware (UseStaticFiles())
  • This file middleware does not see the index.html page (I think?) and returns 404
  • The SPA middleware does not expect this and blows up

Goal

I think the right fix would be to figure out why the index.html is not visible for the local static file middleware and why it returns 404. Though if you remove the SPA middleware, the local static files middleware also returns 404. Not sure if this is a bug still.
Alternatively, we can also throw out the SPA middleware and write a custom - its function is to redirect all requests to index.html so that the SPA page loads so this should be fairly simple (a single Use() might just do it).

But be careful, I quickly tried to fix this here: #4287 and the service stopped doing 500s but the Azure Container App healthchecks started failing (I guess they use OPTIONS?). So this needs to be tested in INT before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant