[Gambas-user] How to stop a gb.inotify watch

Tobias Boege tobs at taboege.de
Fri Oct 29 19:54:51 CEST 2021


On Fri, 29 Oct 2021, james at lixce.com wrote:
> In my gb.qt5.webview  based web browser I'm starting a file watch with:
> 
> wchLink = New Watch("/tmp/Cntrl_lixce-nexus", True) As "extCntl"
> 
> and then monitor for other programs to write a URL to that file.
> 
> Everything works great but I cannot figure out how to stop the watch so that I can close the program gracefully. Tried using a pipe instead but it slows the browser to an absolute crawl, and I am avoiding dbus.
> 
> I was using:
> 
> Public Sub Form_Close()
>   Try Kill "/tmp/Cntrl_lixce-nexus"  'segfaults with or without this line
>   Wait
>   Quit
> End
> 
> Which effectively forces the program to exit but it is very ugly ending in a segfault.
> 

A Gambas program should not segfault at all if you're not playing with
its memory directly. This is probably a bug worth diagnosing, making a
minimal reproducing example for and then reporting on the bugtracker.

> I'm currently using:
> 
> Public Sub Form_Close()
>   Try Kill "/tmp/Cntrl_lixce-nexus"
>   Wait
>   Exec ["kill", Application.Id]
> End
> 
> Which is a little more graceful (no segfault) but is still pretty ugly.
> 
> The inotify man 7 page shows an "inotify_rm_watch" command but I don't know of any way to call it from Gambas.
> 

The watch is removed with inotify_rm_watch when the Watch object gets
destroyed. This happens automatically in Gambas when its reference count
drops to zero. If wchLink is the only variable holding a reference, then
you do wchLink = Null to force the destruction and removal of the watch.
If no more watches are active, then gb.inotify will release the backing
inotify file descriptor and your program will be able to stop.

If your goal is to give other processes a way to send instructions to
your browser, another alternative is using a UNIX server socket from
gb.net. This is also fully event-based. But unlike a file, it allows
bidirectional communication in case you ever need it and you get one
orderly socket per requester instead of having to worry about con-
current writes to a single file.

Best,
Tobias

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


More information about the User mailing list