Skip to main content
5xx Server Error

508 Loop Detected

The server terminated an operation because it encountered an infinite loop while processing a request with "Depth: infinity".

Meaning & Description

The 508 Loop Detected status code is part of the WebDAV extension to HTTP. It indicates that the server terminated an operation because it encountered an infinite loop while processing a request. This usually happens in WebDAV when a client issues a request involving the entire directory structure (using the `Depth: infinity` header), but the server discovers that directories are linked to each other circularly.

Common Causes

  • Symbolic link loops on the underlying filesystem (e.g., Folder A links to Folder B, which links back to Folder A).
  • Cyclic references in database records that cause an infinite recursion when being serialized to JSON.

How to fix a 508 error

  1. If using WebDAV, inspect the filesystem for cyclic symbolic links and remove them.
  2. If encountered in a custom API, trace the recursive function or graph query to identify where the circular reference is being evaluated.
  3. Implement a maximum recursion depth limit in your application logic to fail gracefully before maxing out the call stack.

Browser & SEO Behaviour

Browser Behavior

Displays a generic 500-level error page.

SEO Impact

Treated as a standard server error. Search engines will not index the cyclic resource.

CDN Behavior

Not cached. Passed directly to the client.

Code Examples

HTTP
HTTP/1.1 508 Loop Detected
Content-Type: application/json

{
  "error": "Loop Detected",
  "message": "Infinite recursion detected in resource tree."
}
Node.js (Express)
app.get("/graph/:node", async (req, res) => {
  try {
    // Hypothetical function that might throw a cyclic error
    const data = await resolveGraph(req.params.node);
    res.json(data);
  } catch (err) {
    if (err.name === "CyclicReferenceError") {
      return res.status(508).json({ error: "Loop Detected in Graph" });
    }
    res.status(500).send("Internal Server Error");
  }
});
Python (FastAPI)
from fastapi import FastAPI, HTTPException

app = FastAPI()

@app.get("/tree")
def get_tree():
    # Simulate catching an infinite loop
    infinite_loop_detected = True
    if infinite_loop_detected:
        raise HTTPException(status_code=508, detail="Infinite loop detected.")
Go (net/http)
package main

import "net/http"

func handler(w http.ResponseWriter, r *http.Request) {
	http.Error(w, "Loop Detected", 508)
}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}
C# (ASP.NET Core)
[HttpGet("process")]
public IActionResult ProcessTree()
{
    // Logic detecting a circular reference
    return StatusCode(508, "Loop Detected");
}
curl
curl -i -X PROPFIND -H "Depth: infinity" https://webdav.example.com/files/

Raw HTTP Response Example

HTTP Response
HTTP/1.1 508 Loop Detected
Content-Type: text/plain

Operation terminated due to an infinite loop.

Real-world Examples

WebDAV Servers: Used by servers implementing RFC 5842 (WebDAV binding extensions) when they detect a loop caused by bindings/symlinks.

Frequently Asked Questions

Is 508 Loop Detected the same as a redirect loop?

No. A redirect loop (e.g., getting stuck bouncing between two URLs) results in the browser throwing an `ERR_TOO_MANY_REDIRECTS` error locally. A 508 error is explicitly returned by the server because its own internal logic or filesystem looped.

Can I use 508 for cyclic JSON serialization errors?

Yes, while originally for WebDAV, 508 is highly descriptive and perfectly suited for APIs that catch circular references in data structures and want to explicitly inform the client.

Did You Know?

The 508 status code was introduced in RFC 5842 specifically to deal with the complexities of creating hard links and bindings in WebDAV, which easily create infinite directory trees.

Without the 508 error catch, server applications usually crash with a "Maximum call stack size exceeded" or "Stack overflow" exception, resulting in a generic 500 error.

Developer Tips

  • If you are building an API that allows clients to define complex relationships (like a CMS or graph database), always implement cycle detection algorithms and return 508 if a cycle is found, rather than letting the server crash.

Interview Questions

These are questions you might face in a backend or API design interview that touch on HTTP 508.

What is the difference between a browser throwing ERR_TOO_MANY_REDIRECTS and a server returning a 508 Loop Detected?

Too many redirects is a client-side realization that it has received too many 3xx redirect responses. A 508 Loop Detected is a server-side realization that its internal processing (like traversing a filesystem) is stuck in an infinite loop.

Common Interview Mistakes

  • Stating that 508 is returned when a client sends an infinite number of requests. That would be handled via rate-limiting (429) or WAF blocks.