Skip to content

Commit

Permalink
fix: mounted apps don't eagerly match path prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Jan 22, 2025
1 parent f96c0a0 commit a026bf7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/_bentoml_impl/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

from ..tasks import ResultStatus
from ..tasks import Sqlite3Store
from .mount import PassiveMount

if t.TYPE_CHECKING:
from opentelemetry.sdk.trace import Span
Expand Down Expand Up @@ -178,7 +179,7 @@ def __call__(self) -> Starlette:
app.add_route("/schema.json", self.schema_view, name="schema")

for mount_app, path, name in self.service.mount_apps:
app.mount(app=mount_app, path=path, name=name)
app.router.routes.append(PassiveMount(path, mount_app, name=name))

if self.is_main:
if BentoMLContainer.new_index:
Expand Down
27 changes: 27 additions & 0 deletions src/_bentoml_impl/server/mount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from __future__ import annotations

from starlette.applications import Starlette
from starlette.routing import Match
from starlette.routing import Mount
from starlette.types import Scope


class PassiveMount(Mount):
"""A subclass of mount that doesn't match the path prefix eagerly."""

def matches(self, scope: Scope) -> tuple[Match, Scope]:
match, child_scope = super().matches(scope)
if match == Match.FULL and isinstance(self.app, Starlette):
scope = {**scope, **child_scope}
partial_match: Match | None = None
for route in self.app.routes:
child_match, _ = route.matches(scope)
if child_match == Match.FULL:
return child_match, child_scope
if child_match == Match.PARTIAL and partial_match is None:
partial_match = child_match

if partial_match is not None:
return partial_match, child_scope
return Match.NONE, child_scope
return match, child_scope

0 comments on commit a026bf7

Please sign in to comment.