I have not really looked at it yet, but just as a quick thought:
I am not sure if/why I should debug an infloop, because the functionality
works perfectly fine when not showing the margins (i.e. when just setting
the window margins to nil). So it seems to me its not about an infinite loop.

I was saying 'more or less' because it does not really hang, it just takes
extremely long to update the buffer text. When waiting long enough,
then it seems to continue.

Of course, I don't want to waste your time, but I think my instructions
for reproducing the error, are very clear and short this time (i.e. take less
than/about a minute). I think it would be most informative if you reproduce the
error there.

That's a huge file.

The file is huge because I've added a lot of data in the file (i.e. the text
 of the book `An Introduction to Programming in Emacs Lisp`).
I've mentioned it exactly at the point where you write 'that file is huge'
in your previous message.

Of course, I will have a look at etc/DEBUG now, but I hope you can find
the minute to reproduce the error following my instruction (I guarantee
that the steps for reproducing are very clear and brief).

On Fri, 24 Feb 2023 at 20:41, Eli Zaretskii <eliz@gnu.org> wrote:
> From: dalanicolai <dalanicolai@gmail.com>
> Date: Fri, 24 Feb 2023 20:29:02 +0100
>
> I am encountering the problem as described in the subject of this email.
>
> I hope you could enlighten me again about this issue.
>
> Some brief context:
> I am trying to build a kind of swiper/swoop for searching documents.
> I have tried to use Ivy/Vertico (see https://github.com/minad/consult/issues/625),
> but I think they are not well suited/too heavy for this. So, I concluded that,
> unfortunately, I have to 'reinvent the wheel'.
>
> Anyway, to reproduce the issue starting from emacs -q,
> please load the attached file and do `M-x baleen` (the file is large because
> it includes the text of `An Introduction to Programming in Emacs Lisp`).

That's a huge file.

> Then type 'ho' and optionally continue typing to narrow down the results.
> Press RET to finish (I am just starting development, so never mind further
> 'testing' the current functionality. Although of course, suggestions for
> improvements are welcome).
> As you will find, hopefully, things seem to work fine.
> Page numbers are nicely displayed in the margins.
>
> Now, do `M-x baleen2`. This time, the data consists of the contents of the
> the book `An Introduction to Programming in Emacs Lisp.' Now type e.g.
> 'emacs' to narrow down the results. The page numbers are not displayed
> in the margins, to show that things still work fine.
>
> NOTE the next step should (well, should not of course) make Emacs hang:
>
> Now just uncomment the line
>   ;; (set-window-margins nil 4)
> in the function `baleen2` and repeat the last step (`M-x baleen2`).
> Again, try to type 'emacs'. Unfortunately, Emacs more or less hangs.
>
> I have no idea why suddenly Emacs hangs. I was wondering if any of
> you could explain this behavior, and hopefully suggest how to
> prevent it.
> (Of course, if there is no other solution, then I could just display the
> page numbers in the buffer text).
>
> I also have a second question, but I will send it in another mail (with its
> own subject).

Did you try yourself to establish where Emacs loops and why?  The file
etc/DEBUG includes instructions for how to debug what looks like an
infloop; search for "If the symptom of the bug is that Emacs fails to
respond".

I notice that you say "more or less hangs".  What does this mean,
exactly?  Does C-g interrupt the "hang"?