Repository navigation
fix(SimpleW): map module routes and the not found handler without expression trees - #17
Closed
christallire wants to merge 1 commit into
Closed
christallire wants to merge 1 commit into
christallire wants to merge 1 commit into
Conversation
…ression trees StaticFilesModule, WebSocketModule and ServerSentEventsModule mapped their handlers through MapGet/Map(Delegate), and Router built its not found handler with RouteExecutorFactory.Create(Delegate). Both compile an expression tree from reflection metadata. Under NativeAOT that metadata is trimmed: Expression.Property(session, "Request") throws ArgumentException while the module installs, so a server that uses one of these modules cannot start, and an unmatched request answers 500 instead of 404. They now map a plain HttpRouteExecutor, which runs the same handler without reflection. Behaviour is unchanged under JIT.
Owner
|
Hi @christallire, Thanks for the contribution and the detailed investigation ! I hadn't really planned for NativeAOT support in SimpleW yet, but these changes make sense. |
Owner
|
I pulled your commit and made the other changes you suggested, you can check the testing branch. I've also published an alpha version 26.1.2-alpha.20261004-2289 which includes these changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Under NativeAOT, a server that uses
StaticFilesModule,WebSocketModuleorServerSentEventsModulefails to start. Without them, an unmatched request answers 500 instead of 404.These modules map their handlers through
MapGet/Map(Delegate), andRouterbuilds its not found handler withRouteExecutorFactory.Create(Delegate).Createcompiles an expression tree, which needs reflection metadata that NativeAOT trims. It throws while the module installs:Fix
The three modules and the not found handler now map a plain
HttpRouteExecutor, for exampleserver.Router.Map("GET", path, (session, _) => HandlerAsync(session)). This runs the same handler without reflection. Under JIT, behaviour is unchanged:Map(Delegate)already compiled these handlers down toFromValueTask(handler(session)).Verification
Existing tests (net10.0). 373 of 374 pass. The one failure,
FileBrowserModuleTests.Upload_Direct_Should_Preserve_Directories_And_Replace_File, fails the same way on unpatchedmaster(a file lock on Windows).NativeAOT (win-x64). I used a small console that installs the three modules and sends four requests:
ArgumentException(above)GET /index.html(static)HEAD /index.htmlGET /wswithout upgradePOST /missing(no route)The same console gives the results in the right-hand column under JIT, with and without this change.
Possible follow-ups (not in this PR)
MapFallback(HttpRouteExecutor): an AOT-safe fallback overload. Today onlyMapFallback(Delegate)exists.[RequiresUnreferencedCode]/[RequiresDynamicCode], so that AOT users get a warning at the call site instead of a failure at runtime:RouteExecutorFactory.Create(Delegate);Map*(Delegate);MapController/MapControllers;SystemTextJsonEnginewithout aJsonSerializerContext.SimpleWServer's defaults keep those reflection paths in a trimmed app even when they are never called. These are the defaultControllerActionExecutorFactory(CreateControllerActionExecutor) and the defaultJsonEngine.GitHub is a mirror, so feel free to pull the branch as you prefer.