Urgent.News

What's breaking now, across thousands of outlets.

Tech

"A second operation was started on this context" — the EF Core bug that only shows up under load

"A Second Operation Was Started on This Context" — The EF Core Bug That Only Shows Up Under Load You’ve debugged this error before: InvalidOperationException: A second operation was started on this context before a previous operation completed. It’s frustrating because it works locally but fails under load . The root cause isn’t what you might think—it’s not about explicit Task.WhenAll calls or…

In a recent development, a second operation was initiated regarding the EF Core bug that manifests under load conditions. Developers have encountered this issue before, known as InvalidOperationException: A second operation was started on this context before a previous operation completed. Despite working locally, the problem arises when the application faces actual production load.

The root cause lies in a service-lifetime design issue that only becomes apparent under real concurrency. The problem stems from a fire-and-forget context leak in a hosted background service. A single scoped DbContext is being reused across parallel work items, but what is being held onto is the context itself after its scope has expired. This leaves the context in an invalid state, leading to the error when subsequent operations attempt to use it.

Local testing environment lacks the necessary parallelism to reproduce this bug. Errors appear intermittently, as the context's internal state gets corrupted over time, making the error seem random. Fire-and-forget tasks are often overlooked in debugging because they are out of sight and out of mind.

A common misconception is that EF Core's thread-safety rules apply to explicit parallelism, such as Task.WhenAll. However, EF Core's thread-safety is actually about lifetime. A DbContext must only live within its scoped lifetime. If a task holds onto it after the scope ends, the context's internal state becomes invalid, leading to the bug.

To resolve this issue, the correct approach is to ensure one scope per unit of work. The context should be resolved inside the scope, not per service instance. Fire-and-forget tasks must not capture the context in their closure. If such tasks are used, the context should not be captured after the scope ends. Additionally, explicit management of scopes is recommended to ensure the context is disposed when the work is done.

The significance of this issue goes beyond just fixing an error. It's about designing applications for parallelism. EF Core's concurrency rules are centered around respecting dependency boundaries. Violating these boundaries, such as letting a DbContext outlive its scope, will lead to similar errors. To avoid these problems, developers should test under load and design for lifetime correctness. By following these principles, they can create more robust and scalable applications.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

CVE-2026-92951: CVE-2026-92951: Sandbox Escape via External Package Allowlist Bypass in vm2

CVE-2026-92951: Sandbox Escape via External Package Allowlist Bypass in vm2 Vulnerability ID: CVE-2026-92951 CVSS Score: 9.9 Published: 2026-10-01 An incorrect authorization and directory traversal…

  • Remote attackers can bypass sandbox in vm2, execute arbitrary host code.
  • Incorrect authorization and directory traversal in external package allowlist.
  • Upgrade to vm2 version 3.11.7 or later to fix vulnerability.

Understanding Prototypes in JavaScript: A Practical Guide for Developers

JavaScript uses prototypes to support inheritance, property lookup, and shared behavior between objects. Although modern syntax gives us classes and familiar object-oriented patterns, prototypes…

  • JavaScript uses prototypes for inheritance and shared behavior among objects
  • Every object has an internal prototype reference for property lookup
  • Prototype chain enables shared methods across instances and constructor functions

Structured Logging and Distributed Tracing: Moving Beyond console.log

We have all been there during an outage: staring at terminal logs searching for an error: [2026-09-25 02:14:12] Error processing request: object is None [2026-09-25 02:14:12] Failed to charge credit…

  • Structured JSON logging provides machine-readable, queryable format for complex systems
  • Correlation IDs enable tracing request lifecycle across multiple services
  • Middleware implementation facilitates attaching correlation ID to requests in frameworks

Trying unknown Mac software without trusting it: a disposable macOS VM on Apple Silicon

At some point you want to try a Mac app you don't fully trust: a menu bar utility from a one-person shop, an installer someone linked in a forum, a CLI that wants sudo .

  • Disposable macOS VM on Apple Silicon tests unknown software safely.
  • VM provides separate macOS environment without affecting host system.
  • Requirements include Apple Silicon Mac, macOS 14, and compatible VM application.

An uncalibrated classifier is worse than no classifier

What I learned building a website that rewrites itself for whoever is reading it, on ₹0 of infrastructure. Project Blog : https://santhosh-reddy.vercel.app/en/blog/8 Project Breakdown…

  • Uncalibrated classifier performs worse than no classifier
  • Poorly calibrated traffic split leads to slower convergence
  • Calibration solution over raw accuracy improvement

More from Friday 2 October →