Stack size limit with findall

I’m using: SWI-Prolog version 8.2.1
I have 16GB RAM ubuntu 20
I want the code to:
When i call in the first i don’t hav any error i make a query with findall at secondi have this error :
But what I’m getting is:
[Thread httpd@8082_1] POST /prove: [503] Stack limit (1.0Gb) exceeded
Stack sizes: local: 50Kb, global: 38.3Mb, trail: 1Kb
Stack depth: 27, last-call: 33%, Choice points: 141
[27] system:’$add_findall_bag’([length:2])
[26] ‘$bags’:findall_loop([length:2], ‘<garbage_collected>’, _572, [])
[25] system:setup_call_catcher_cleanup(<compound (:)/2>, <compound (:)/2>, _602, <compound (:)/2>)
[21] ‘$bags’:bagof(’<garbage_collected>’, ‘<garbage_collected>’, _644)
[20] user:my_findall(’<garbage_collected>’, ‘<garbage_collected>’, _666)

    Use the --stack_limit=size[KMG] command line option or
    ?- set_prolog_flag(stack_limit, 2_147_483_648). to double the limit.

set_prolog_flag(stack_limit, 2_147_483_648) don’t resole the probleme
My code looks like this:


Can you provide a Minimal and reproducible working example



looks wrong. I would expect the Template to have a variable from the Goal. See findall/3.

findall/3 execution doesn’t use much stack as it is backtracking, but at the end it needs to put all solutions in a list. While collecting the answers it computes how much stack it will need in the end and if that exceeds the limit it will abort with a stack overflow while the stack itself still seems small (as the first post indicates).

What to do depends on what is called. Possibly just enlarging the stacks enough fixes the problem. Possibly there is a mistake in your program and the number of solutions is infinite or simply far too large. Possibly the result set is just very big and you really want the result with limited resources. In that case you need some streaming way to process the answers. What and how depends on the overall scenario. If the findall/3 call collects answers to send back as JSON this is unlikely to help as the browser will also try to read the entire JSON term and thus also run out of memory.

But during my first call I receive the data certainly there is a lot anyway but during the second call that’s when the memory problem arises.

With the first two calls I get an answer

Who knows … If these are exactly the same calls I see two likely options: your call has side effects (assert, etc) that make the next behave differently or you are close to the limit. As you get close to the stack limit the system may sometimes signal an overflow and sometimes not, depending on the history.

Otherwise we have very little to go on. It runs out of stack in findall/3. We know it calls Term, but have no clue what that is, nor what the program called by Term does. For debugging you can also call your mystery Term on the interactive session and see how many solutions it has and how big it is (see length/2 and term_size/2).

Note that if you use the Prolog flag stack_limit you must set it before starting the HTTP server or e.g., using swipl --stack-limit=4g. Setting it later doesn’t have any effect as the stack limit is set for the calling thread and threads created from it and the HTTP threads have already been created.

The probleme is before calling findall i doing some assert that way we have the error now after the assert i retract all assert .

THanks .