Can't close `fetchEventSource` with `AbortController` in React
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Da chiarire
- Stato di attività
- Ferma
- Stack tecnologico
- react, typescript
- Ambito
- frontend, networking
Direzione di ricerca
Inizia con la riproduzione in React.StrictMode e la chiamata a fetchEventSource usando AbortController.signal. Traccia se l'interruzione durante la pulizia chiude la prima richiesta SSE e confronta il comportamento del browser con il log http.disconnect del backend; il lavoro è concluso quando la connessione duplicata viene chiusa in modo affidabile durante l'unmount oppure il problema viene ricondotto a una limitazione documentata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Hello. I'm aware this topic has been raised many times here already, and I have read all of those issues with no success. Indeed, in most cases those issues have comments from people saying the proposed solution didn't work for them either. I am running my app in React.StrictMode, which causes each component to mount/unmount/remount to help identify unexpected side-effects and so on. In my case this is causing two fetchEventSource requests to be fired, which is expected. What's unexpected, is that using an AbortController does not close the first connection on unmount. This is my hook:
const useStreamTradingNotifications = () => {
const [ abortController, setAbortController ] = useState(new AbortController());
useEffect(() => {
console.log("useEffect called");
const stream = async () => {
await fetchEventSource("http://127.0.0.1:8000/v1/notifications", {
signal: abortController.signal,
onmessage(msg) {
if (abortController.signal.aborted){
console.log("still getting messages from aborted signal")
}
console.log(`event: ${msg.event}`)
if (msg.event == "update") {
console.log(JSON.parse(msg.data))
}
},
});
};
stream();
return () => {
console.log("abort called")
abortController.abort();
setAbortController(new AbortController());
}
}, []);
};
When I run my app, I see the following logs:
As you can see:
useEffectis called twice, as the component is mounted/unmounted/remounted [expected]abortController.abortis called when the first component unmounts [expected]- Subsequently I receive two of each message (the logs above are truncated, and just includes the first duplicate). For one of the two streams, I can confirm that the messages are being received on an already-aborted connection (
still getting messages from aborted signal).
Furthermore, in my backend logs, when I close a browser window with an active connection I see a log like:
[2024-07-09T11:13:37.409247Z][sse_starlette.sse][DEBUG] Got event: http.disconnect. Stop streaming.
But I never see this message as the result of AbortController.abort being called. I have tried assigning a new instance of AbortController after abort (as suggested here), which had no affect. I have tried doing a similar thing with setState (as mentioned here) which made the problem much worse (kept creating connections until the browser crashed). I tried to use useRef to manage the controller (suggested here, which also doesn't work.
The fact that I can see abort being called, but never see a http.disconnect on the backend, convinces me this must be a bug. But if anybody has a suspicion that I've screwed something up I'd really appreciate their suggestion.
Many thanks
- Lingua principale
- TypeScript
- Stelle
- 2.9k
- Fork
- 197
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Azure/fetch-event-source
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 28/100
Azure/fetch-event-source#102 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 25/100
Azure/fetch-event-source#101 · 3 commenti ·
-
When aborting the event source using abort controller, no callbacks (onError / onClose) are firedAperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 48/100
Azure/fetch-event-source#100 · 1 commento · 5 reazioni ·
-
Weird Vite chunk map sourcesAperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
Azure/fetch-event-source#99 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
Azure/fetch-event-source#98 · 9 reazioni ·
Tutte le issue di Azure/fetch-event-source
Issue simili
-
ready-for-agent
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
xavxyz/bei-shan-lin-shui#57 ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG] rag: sentence content can cross the reported sentence boundariesForse già presa @LJK-ikun l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
bug needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 1 giorno
-
README: the Resource URI Schemas example's file://documents/* pattern only matches that literal URIAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
platformatic/mcp#361 ·
I maintainer di solito rispondono entro 1 giorno
-
最近文档这个弹窗,左右宽度没有最小宽度限制Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
siyuan-note/siyuan#20799 · 7 commenti ·
I maintainer di solito rispondono entro 1 giorno