Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

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

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Ajax

Ajax: Fix a Fetch Call That Silently Ignores Your Abort Signal Because the Controller Was Created After the Request Already Started

- 04.10.26 - ErcanOPAK

🔄 The Abort Signal Nobody Was Listening For

Canceling an in-flight fetch request depends entirely on passing the abort controller’s signal into the request before it starts, since the signal has to already be attached for the browser to know which in-flight request a later abort call is supposed to interrupt. A controller created and aborted from inside a callback that runs after the fetch call has already been issued – a very easy mistake when the controller is built lazily, inside the same function that triggers the request – never gets attached to anything, so calling abort on it does precisely nothing, and the original request keeps running to completion, with its response quietly applied to the page even though the code clearly intended to cancel it.

🔎 The Problem

function search(query) {
    // Looks correct - a fresh controller for each search call:
    const controller = new AbortController();

    if (activeSearch) {
        activeSearch.abort(); // aborts the PREVIOUS controller - fine
    }
    activeSearch = controller;

    // The bug: the signal is read from `controller` here, but if
    // this function is re-entered before the fetch below actually
    // starts (e.g. called again synchronously, or from a debounced
    // handler that fires twice), the second call's fetch captures
    // a DIFFERENT controller's signal than the one `activeSearch`
    // now points to - so aborting activeSearch later aborts a
    // controller no in-flight fetch call ever actually used.
    return fetch(`/api/search?q=${query}`, { signal: controller.signal })
        .then(r => r.json());
}

✅ Fix: Create and Attach the Controller Before Anything Else Can Run

  • Create the abort controller and read its signal into the fetch call in the same synchronous block, before any other code – including a debounce wrapper or an event handler re-entry – gets a chance to run and start a second request with a different signal.
  • Store the exact controller instance that was actually passed into the in-flight fetch call, not a separately tracked reference that could point to a different one, so the later abort call is guaranteed to target the request it is meant to cancel.
  • Add a quick console log of the controller’s identity at both the point it is created and the point abort is called on it during development, to catch a mismatch between the two immediately instead of only noticing a stale response applying itself to the page.

⚠️ Why This Is Easy to Miss

  • The code reads top to bottom as if it obviously cancels the previous request before starting a new one, and it does exactly that for the common case of two calls spaced far enough apart – the bug only appears when calls happen close enough together to race.
  • A fetch call that never actually gets canceled simply finishes and applies its result, which looks like a normal, successful search response – there is no error or rejected promise to point at the fact that an abort call silently had no target.

An abort signal only cancels the request it was attached to – if two requests existed before the attaching happened, aborting one was never going to touch the other.

— Frontend Engineer

Related posts:

Ajax: Axios vs Fetch — Which One to Use?

Ajax: Axios vs Fetch — Which One to Use?

Ajax: Fix an XMLHttpRequest Progress Event That Never Fires Because the Response Is Missing a Conten...

Ajax: Fix an XMLHttpRequest Progress Event That Never Fires Because the Response Is Missing a Conten...

Ajax: Understanding CORS and How to Fix It

Ajax: Understanding CORS and How to Fix It

Views: 10
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Photoshop: Fix a Layer Mask That Inverts Itself the Moment You Apply a Smart Filter on Top of It
C#: Fix a Source-Generated Logging Message That Still Evaluates Its Arguments Even When the Log Level Is Disabled

Leave a Reply Cancel reply

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

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 999 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 998 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 874 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 673 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 632 views
  8. 08Average of all values in a column that are not zero in SQL 586 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 563 views
Bits of .NET

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

Social

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