Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Ajax

Ajax: Fix a Long-Polling Request Silently Piling Up Duplicate Connections Because the Previous One Was Never Aborted Before Starting the Next

- 04.11.26 - ErcanOPAK

🔄 Fix a Long-Polling Request Silently Piling Up Duplicate Connections Because the Previous One Was Never Aborted Before Starting the Next

Long polling works by holding a request open until the server has something new to report, then immediately firing off a fresh request to go back to waiting — and that “immediately fire off a fresh request” step is exactly where a missing cleanup step causes trouble. If the code that starts a new long-poll request doesn’t first make sure the previous one is actually finished (or explicitly abort it when it isn’t), a reconnect triggered by something other than a clean server response — a page regaining focus, a network blip, a reconnect button — can leave the old request still open on the server side while a brand new one starts stacking on top of it, and the browser quietly accumulates open connections that never get cleaned up until they individually time out.

🔎 The Problem

// Bug: no record of the in-flight request, nothing to abort
function startLongPoll() {
  fetch('/poll').then(res => res.json()).then(data => {
    handleUpdate(data);
    startLongPoll();            // chains cleanly on a real response
  });
}

window.addEventListener('focus', startLongPoll);
// Every time the tab regains focus, this starts ANOTHER poll loop
// on top of whichever one (if any) is already running - nothing
// here ever stops the previous one.

✅ The Fix

  • Keep a single module-level AbortController for the current long-poll request, and call .abort() on the previous one before creating a new controller and starting a fresh request, so there’s never more than one in-flight poll at a time.
  • Guard the reconnect trigger itself with a simple in-progress flag (or check the AbortController’s signal) so a focus event or reconnect button firing while a poll is already active doesn’t start a second loop stacked on top of the first.
  • Watch your server’s open-connection count during a quick manual test where you rapidly switch tabs or toggle network conditions — a correct implementation’s connection count stays flat; a leaking one climbs with every reconnect trigger.

🚨 Why This Is Dangerous

  • Each duplicate connection consumes a server-side worker, socket, or thread for as long as it stays open, and on a server with a fixed connection pool, enough accumulated duplicates from enough clients switching tabs can exhaust that pool for everyone, not just the clients that caused it.

Long polling’s whole model depends on there being exactly one open request per client at a time — nothing enforces that for you, and a missing abort is how it quietly becomes two, then three, then more.

— Frontend Engineer

Related posts:

Ajax: Use Fetch API for Modern HTTP Requests

Ajax: Use Fetch API for Modern HTTP Requests

Ajax: Mastering Request Cancellation with AbortController

Ajax: Mastering Request Cancellation with AbortController

AJAX — 204 Responses Break JSON Parsing

AJAX — 204 Responses Break JSON Parsing

Views: 7

Post navigation

Git: Fix a .gitattributes Line-Ending Rule Silently Not Reformatting Already-Committed Files Because Normalization Only Applies Going Forward Until You Renormalize
JavaScript: Fix a Debounced Function Still Firing Once per Call Instead of Once per Burst Because the Timer Variable Is Declared Inside the Returned Function Instead of the Outer Closure

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (607)
  • Average of all values in a column that are not zero in SQL (569)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com