Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🐛 Ensure thatapp.include_router merges nested lifespans#9630

Merged
tiangolo merged 20 commits intofastapi:masterfrom
Lancetnik:master
Aug 24, 2024
Merged

🐛 Ensure thatapp.include_router merges nested lifespans#9630
tiangolo merged 20 commits intofastapi:masterfrom
Lancetnik:master

Conversation

@Lancetnik
Copy link
Contributor

@LancetnikLancetnik commentedJun 6, 2023
edited
Loading

I am working on my ownPropan library and as a part of it I implemented a customFastAPI router with the following behavior.

fromfastapiimportFastAPIfrompropan.fastapiimportRabbitRouterrouter=RabbitRouter("amqp://guest:guest@localhost:5672")app=FastAPI()app.include_router(router)

To connect RabbitMQ atFastAPI startup I usedrouter.on_startup events and all worked fine, but now it is deprecated. So I migrate tolifespan and nowFastAPI doesn't run router lifespan at the code above.

I suppose it was missed cuz the originalinclude_router method includes all nestedon_startup andon_shutdown deprecated eventshere, but doesn't includes a router lifespan.

So, the following code doesn't work

fromcontextlibimportasynccontextmanagerfromfastapiimportFastAPI,APIRouter@asynccontextmanagerasyncdefrouter_lifespan(app):print('router start')yieldprint('router shutdown')router=APIRouter(lifespan=router_lifespan)app=FastAPI()app.include_router(router)

The current PR fixes the original framework miss.
All original test and lint.sh checks works fine.

xKlask, Alurith, Alexandrhub, kiriharu, bio-boris, didhat, MatCatLeroux, iagoneres, hirotasoshu, nicocti, and 35 more reacted with thumbs up emoji
@github-actions
Copy link
Contributor

📝 Docs preview for commit860c851 at:https://647f7cdb633e285f9b5dace9--fastapi.netlify.app

@LancetnikLancetnik changed the titleFix:app.include_router doesn't nest lifespansFix:app.include_router doesn't merge nested lifespansJun 6, 2023
@github-actions
Copy link
Contributor

📝 Docs preview for commit984e59f at:https://647f7f0b29b2f764bcaa3dd9--fastapi.netlify.app

@tiangolo
Copy link
Member

📝 Docs preview for commit8f26ed5 at:https://6484cca7bf187a105b276a5e--fastapi.netlify.app

@Lancetnik
Copy link
ContributorAuthor

@tiangolo can you take a look here, please? It's a minor bugfix, but it's important for my project.

@LancetnikLancetnik changed the titleFix:app.include_router doesn't merge nested lifespansBugfix:app.include_router doesn't merge nested lifespansJun 16, 2023
@tiangolo
Copy link
Member

📝 Docs preview for commitf322c99 at:https://648c2ec07c8ac9541405829c--fastapi.netlify.app

@tiangolo
Copy link
Member

📝 Docs preview for commit3e2fd98 at:https://648e16b538df29410a152328--fastapi.netlify.app

@Kludex
Copy link
Member

@tiangolo can you take a look here, please? It's a minor bugfix, but it's important for my project.

This is not minor, and ideally we'd like this to live in Starlette.

@Lancetnik
Copy link
ContributorAuthor

@Kludex maybe, but how we can move it to Starlette ifinclude_router is theFastAPI-level method? Do you suggest move the method to Starlette?

@Lancetnik
Copy link
ContributorAuthor

This will require a lot of work in bothFastAPI andStarlette, since none of theFastAPI methods usessuper().add_route() fromStarlette. Therefore, it is necessary to rewrite all the methodsadd_api_route,add_websocket_route,add_api_websocket_route to use the parent methods of Starlette. It will also require a consistent update of these two packages. It seems to me that this is too difficult way, but I will do it. Perhaps it makes sense to merge the current PR for now before we can make a more elegant solution.

@Lancetnik
Copy link
ContributorAuthor

@Kludex, so can you check aStarlettePR?

@Lancetnik
Copy link
ContributorAuthor

This is not minor, and ideally we'd like this to live in Starlette.

@Kludex this bug doesn't onStarlette side and not connected with thisIssue or thisStarlettePR orIssue.

It should be fixed inFastAPI cuz we have noStarlette calls insideinclude_router method. It doesn't useStarletteHost orMount that can be nested atStarlette side, but copies all included router attributes toapp.router directly. So, we should merge the incuded lifespan here too.

So can you review and approve this PR?

@Kludex
Copy link
Member

Starlette should support running the lifespan of other routers... It shouldn't be here.

@Lancetnik
Copy link
ContributorAuthor

Lancetnik commentedJun 23, 2023
edited
Loading

Starlette should support running the lifespan of other routers... It shouldn't be here.

But there noother routers. We have the originalapp.router and copy all routes of included router to it. What do you want to do with it?

@Kludex just take a look atFastAPI original codehere

AFastAPI application works with an only one original router (if we didn't useapp.mount to mount another FastAPI application). So how we can useStarlette lifespan resolving logic here?

@ovflw
Copy link

+1

1 similar comment
@Flosckow
Copy link

+1

@FraterCRC
Copy link

+1)

@ApostolFet
Copy link

+1

@iNerV
Copy link

Wait for fix

@bomzheg
Copy link

+1

1 similar comment
@jekel
Copy link

+1

@Tishka17
Copy link

@tiangolo can we have a minute of your time here?

@Tishka17
Copy link

I am sorry to annoy you, but it is 14 months since PR is opened. Can we, as a community, have a decision or plan?

@Lancetnik
Copy link
ContributorAuthor

@svlandeg@estebanx64@alejsdev Hello, team! Sorry for such annoyiment, but we are still waiting for any decision about this PR.

FastAPI now has an obvious bug / weaknesses. And we have the PR fixing it.

I know, that this thing should be fixed in the future according to Roadmap, but we are waiting year already for it. Have we any reason to not merge 10 lines of code if they fixing the problem already and we can remove them in the future as a part of Roamap feature scope?

krau5, dolfinus, Tishka17, and Mawwlle reacted with thumbs up emoji

@chessenjoyer17
Copy link

+1

3 similar comments
@Alkalit
Copy link

+1

@Cub11k
Copy link

+1

@chirizxc
Copy link

+1

@prostomarkeloff
Copy link
Contributor

@dmitryleafall
Copy link

+1

@fastapifastapi locked asspamand limited conversation to collaboratorsAug 7, 2024
@KludexKludex added p1 and removed p4 labelsAug 7, 2024
@svlandegsvlandeg changed the titleBugfix:app.include_router doesn't merge nested lifespans🐛 Ensure thatapp.include_router merges nested lifespansAug 23, 2024
@svlandegsvlandeg added the bugSomething isn't working labelAug 23, 2024
@tiangolo
Copy link
Member

Thank you for your contribution@Lancetnik! 🍰

Thanks everyone for the input. ☕

Thanks a lot for the help, as always,@Kludex 🙏 🙇

This will be available in FastAPI version0.112.2, released in the next few hours. 🎉


Thanks everyone for the patience. As this involved some internal changes, that means there's a bit of a higher chance it could break something else elsewhere (something we didn't expect and didn't account for), so I wanted to handle some other lower-impact issues and releases first.

Now I was finally able to sit a few hours dedicated just to this, read several parts of the ASGI spec again, ensure this would be the right way to handle things (e.g. what nested lifespan states should override which), etc.

Now, time to enjoy your new nested lifespans. 😎 ☕

@tiangolotiangolo merged commit3a4ac24 intofastapi:masterAug 24, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@KludexKludexKludex approved these changes

+1 more reviewer

@Tishka17Tishka17Tishka17 approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

bugSomething isn't workingp1

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

20 participants

@Lancetnik@tiangolo@Kludex@logankaser@Tishka17@abdullahsych@Praneethvvs@coryphoenixxx@IvanKirpichnikov@theseriff@KrySeyt@chessenjoyer17@PlzTrustMe@krau5@lubaskinc0de@ovflw@Flosckow@FraterCRC@ApostolFet@iNerV

Comments


[8]ページ先頭

©2009-2026 Movatter.jp