crawl4ai icon indicating copy to clipboard operation
crawl4ai copied to clipboard

[Bug]: Memory Leak on Repeated /md Requests via Docker (MacOS) — Container Crashes Randomly Over Time

Open Parth7030 opened this issue 1 year ago • 15 comments

crawl4ai version

0.6.0-r2 or Latest docker image

Expected Behavior

When sending repeated POST requests to /md every 10 seconds with dynamic URLs, the Docker container should handle each request efficiently without memory leaks or crashes, maintaining stable memory usage over time.

Current Behavior

Initially, everything works fine. However, after some time (randomly, depending on duration and frequency), memory usage starts increasing significantly until the container crashes. This suggests a possible memory leak or improper cleanup of resources on repeated executions.

Attached below is a screenshot showing the memory spike before the crash.

Is this reproducible?

Yes

Inputs Causing the Bug

Example request payload sent via Postman every 10 seconds:

{
  "url": "http://www.example.com",
  "f": "fit",
  "q": null,
  "c": "0"
}

Steps to Reproduce

1.	Run the crawl4ai app using the Docker setup.
2.	Use a tool (like Postman or a script) to send POST requests to http://localhost/md every 10 seconds.
3.	Use a changing URL in the request payload for each call.
4.	Monitor Docker memory usage over time.
5.	Observe the steady increase in memory and eventual container crash.

Code snippets


OS

macOS Sequoia Version 15.5

Python version

3.12

Browser

No response

Browser version

No response

Error logs & Screenshots (if applicable)

Image Image

Parth7030 avatar Jun 29 '25 01:06 Parth7030

yeah I have same issues as well.

tanushshukla avatar Jun 29 '25 22:06 tanushshukla

We are experiencing the same issue and I would like to provide some additional details:

  1. I have attached the logs for your review.
  2. Notably, when the container is hanging, the /health endpoint still returns a status of 200.

Please let me know if you need any further information.

logs-from-crawl4ai-in-crawl4ai-575d75bc87-bljrk.log

emyco avatar Jul 02 '25 18:07 emyco

Is there a current workaround for this? I notice this in 0.6.0 as well as in 0.6.3 (built into docker image).

NuoWenLei avatar Jul 04 '25 04:07 NuoWenLei

Also a tangentially related issue to this thread. In crawler_pool.py since it uses psutil.virtual_memory() to check for memory usage percentage, it returns the host memory usage percentage. I think it would be better if the crawler pool is thresholded based on the container memory usage.

https://github.com/giampaolo/psutil/issues/1011

NuoWenLei avatar Jul 05 '25 05:07 NuoWenLei

I observed the same issue. Is it possible that all the crawled content like images etc. are simply added up during the multiple calls and never get released, leading to the observed continuous usage of RAM?

Would there be any dirty workarounds for this, such as restarting "the browsers" periodically?

rawinkler avatar Jul 20 '25 15:07 rawinkler

same issue here, only restarting the container helps

Reinstaal avatar Jul 21 '25 13:07 Reinstaal

@unclecode I belive this is a recurrent behaviour in every endpoint. I'm having similar issues on Linux:

  • endpoint: /crawl
  • version 0.7.0

In my case I have an n8n automation calling for this endpoint every ~7 or 8 seconds and it works pretty well for the first ~30 to 50 iterations, but then it just starts returning error 500 (crawl4ai endpoint, not n8n automation). I've registered the time spent on each request and it builds up gradually in a clear pattern (like 1 to 2s every 30 to 50 iterations) until it stops working throwing an "Internal Server Error" and the stack trace having something related to axios.

Sadly I wasnt' able to investigate further

arturbacilla avatar Jul 27 '25 21:07 arturbacilla

Same issue on linux version 0.7.2

SimonMayerhofer avatar Aug 06 '25 10:08 SimonMayerhofer

I looked a bit into the code, and I think there are multiple sources to the memory leak though I couldn't identify all of them. There seems to be some kind of held references of the results even after the API calls because I was able to reduce the memory leak somewhat by emptying the result objects in async_dispatcher.py.

445                    # Process completed tasks
446                    for completed_task in done:
447                        result = await completed_task
+++                        result.result.html = ""
+++                        result.result.fit_html = ""
448                        results.append(result)

Additionally, a temporary workaround I found, though somehow it still doesn't seem to clear all the memory leak on my end, was add a restarter script in the supervisord config that periodically restarts the gunicorn service.

NuoWenLei avatar Aug 19 '25 16:08 NuoWenLei

Good afternoon everyone, in my case, I discovered that I was calling the crawler without closing it properly, which was eating up my memory. For now, the solution I found is this:

class LLMExtractor:
    def __init__(self):
        # crawler initiation
        self.crawler = AsyncWebCrawler()
        self.crawler_config = CrawlerRunConfig(
            delay_before_return_html=delay_before_html, cache_mode=CacheMode.DISABLED
        )

    async def close_crawler(self):
        """Close the crawler to free resources."""
        if self.crawler:
            await self.crawler.close()
            self.crawler = None
            gc.collect()

and whenever crawling is done, I call this close function. Hope it helps you

alitavanaali avatar Sep 02 '25 13:09 alitavanaali

Finally, I thought I am crazy - it eats so much memory and is unstable with the Docker version. Best performance I could get was by setting those settings for my container: env: PLAYWRIGHT_MAX_CONCURRENCY=2 Resource limits: Image then it kills itself, but if used in some workflows you need to detect that it's down, else you get no crawling

bockmurat avatar Sep 15 '25 06:09 bockmurat

Hello everyone! We have made some improvements to our latest Docker image: version 0.7.7 Please use this updated image and let us know if you are still experiencing the same memory issue.

I just tried to reproduce the issue with the code, and here are the results:

Image

ntohidi avatar Dec 08 '25 12:12 ntohidi

Hello @ntohidi , I am using 0.7.8 via docker image and unfortunately I am also hitting some memory issues after performing some requests to the crawl4ai running in my docker container. This is the error I see in the docker logs:

2026-01-09 00:12:41,943 - api - INFO - Memory usage: Start: 318.1484375 MB, End: 318.1484375 MB, Delta: 0.0 MB, Peak: 318.1484375 MB
2026-01-09 00:12:43,220 - crawler_pool - INFO - ♨️  Using hot pool browser (sig=6b188e3f)
[ERROR]... × XXX                           | Error: 
Unexpected error in _crawl_web at line 559 in wrap_api_call 
(../usr/local/lib/python3.12/site-packages/playwright/_impl/_connection.py):
Error: Browser.new_context: Target page, context or browser has been closed

Code context:
 554           parsed_st = _extract_stack_trace_information_from_stack(st, 
is_internal, title)
 555           self._api_zone.set(parsed_st)
 556           try:
 557               return await cb()
 558           except Exception as error:
 559 →             raise rewrite_error(error, f"{parsed_st['apiName']}: 
{error}") from None
 560           finally:
 561               self._api_zone.set(None)
 562   
 563       def wrap_api_call_sync(
 564           self, cb: Callable[[], Any], is_internal: bool = False, title: 
str = None 
2026-01-09 00:12:43,236 - api - INFO - Memory usage: Start: 318.1484375 MB, End: 318.1484375 MB, Delta: 0.0 MB, Peak: 318.1484375 MB

If anyone have any idea how I could resolve this or at least some hacky quickfix or env settings change, I would be very happy to hear them:) Thanks

microHoffman avatar Jan 09 '26 00:01 microHoffman

I tested this on the latest Docker image (v0.8.5) with 70 sequential requests to /crawl using different URLs with cache_mode=bypass:

  • Baseline: 558MB
  • After 10 requests: 870MB (browser warmup)
  • After 50 requests: 870MB (stable)
  • After 70 requests: 871MB (stable)

Memory stabilizes after initial browser warmup and does not grow further. The unbounded growth reported on v0.6–v0.7.8 appears to be resolved by several fixes that shipped in v0.8.0+, including browser context LRU eviction, CDP leak fixes, and browser recycling.

Please upgrade to the latest image and let us know if you still experience issues:

docker pull unclecode/crawl4ai:latest

hafezparast avatar Mar 25 '26 00:03 hafezparast

@hafezparast I have updated to v0.8.6 and I am running the crawl4ai container for more than 12 hours and it still works fine. Previously it crashed a lot faster, so hopefully no more crashes;). I will update here in case it happens to me again.

microHoffman avatar Apr 09 '26 03:04 microHoffman