Steadfastness of a remote answer collector, Web Prolog style

The example uses pengin_spawn/2, pengine_ask/2, pengine_next/1 and receive/1. Where and how is a pengine/webprolog deallocated? What would happen if I do, i.e. ask main/1 with a closed and shorter list. Would main/1 be steadfast and deallocate a pengine/webprolog at the same time?

Welcome to SWI Web Prolog!

?- main([A,B]).

Are there some setup_call_cleanup/3 patterns possible/impossible or necessary/unnecessary for pengines/webprolog? setup_call_cleanup/3 has been introduced in many Prolog systems, since it helps accessing or modifying resources.

There is even a draft standard:
https://www.complang.tuwien.ac.at/ulrich/iso-prolog/cleanup

The typical pattern to access a resource such as a server, is the following programming pattern, which has the advantage that it can be used via backtracking and also works nicely with surrounding cut:

fetch(X) :-
    setup_call_cleanup(server_open(Y),
         server_fetch(Y,X),
         server_close(Y)).

It might not be the prefered programming pattern if between backtracking or cutting away the fetch/1 call, a lot of time can pass, and therefore the server connection would be open for a long time.

But nevertheless, it assures that resources are freed. The cleanup part is also called in case an exception happens in the call part or in the continuation.