Doesn't properly release VTE, causing mem and FD leak

Bug #1645500 reported by Egmont Koblinger
This bug report is a duplicate of:  Bug #785501: Terminator has a (some?) memory leak. Edit Remove
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Terminator
New
Undecided
Unassigned

Bug Description

Found at https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=668238

As one uses Terminator, it opens up to 3 files (and immediately unlinks them) per VTE. These files contain the scrollback data.

When a terminal is closed, VTE is actually not properly released (apparently someone's still holding a reference [perhaps a callback, signal, whatever...]), and in turn its file descriptors are not closed. That is:

- All the disk space used by them to store the (potentially unlimited) scrollback remains occupied for the entire lifetime of Terminator, potentially leading to quickly filling up /tmp;

- There's a limit on the open FDs a process can have, it becomes possible that a user with a long-running Terminator hits this limit.

I've tried explicitly calling gc.collect() but it didn't help, closing these files is not just slightly deferred due to GC's nature, they are not closed at all.

---

How to reproduce:

- Start Terminator with a couple of terminals;

- In each of them produce lot of output including various colors;

- ls -l /proc/[terminator's pid]/fd;

- Notice 3 file descriptors for each VTE pointing to "/tmp/blahblah (deleted)";

- Close some of the terminals;

- Notice that their files are still open.

- If you really produce a lot (like, really a lot) of data in these terminals with infinite scrollback, you can also see "df" reporting a shrinking /tmp.

- If you add a debug printf to vte's vte_terminal_finalize(), you'll see that it's not called.

Expected behavior:

- When a terminal is closed, its 3 file descriptors should disappear from this listing, and correspondingly (at any time, the number of such deleted open files should be at most 3 times the number of open terminals). The available disk space ("df") under /tmp should grow back.

- The debug printf should tell you that vte_terminal_finalize() is executed.

---

A bit of technical background:

Each VTE creates 3 streams for the scrollback contents. One is for the plain text. The other is for colors and other attributes, using a runlength encoding (to--from coordinates for each continuous run of identical attributes). The third one contains pointers to the first two for each pyisical line of the buffer.

Each stream is flushed to disk after every 64kB of data. The file is only created at the first write, that is, when 64kB is already written to that stream.

There's compression, encryption, and a weird mapping to get a circular buffer if the scrollback is finite large, I'm not going into these details here.

If the teminal's contents consists of plain non-colored single-wide characters only, the file containing the attributes won't grow and hence this stream won't hit the disk. Therefore if you wish to see 3 file descriptors appearing, you need to produce various colors, or double-wide (CJK) characters; a giant plain English text will only get you 2 files.

When the terminal's contents are rewrapped on a resize, 1 of the 3 streams (the one with the row indexes) is regenerated and the old one is thrown away (and so is the underlying file). Therefore when a terminal is resized, you might face the number of opened files decreasing by 1.

I'm writing these so that you don't get into too many unknowns and wtfs while debugging :)

Revision history for this message
Egmont Koblinger (egmont-gmail) wrote :

Terminix suffers from this same bug: https://github.com/gnunn1/terminix/issues/589. Other VTE-based terminals I've tried (gnome-terminal, xfce4-terminal, mate-terminal) don't have this issue.

Revision history for this message
Egmont Koblinger (egmont-gmail) wrote :

These file descriptors are just the easiest to see symptoms of the widget not being released. Another symptom is memory leak: the memory footprint of the entire app grows by perhaps another 0.5MB or so after each terminal that's opened, being used for a while and then closed. (I haven't checked it but I'm sure it happens.)

---

A bit of technical background, again:

For each of these three streams, there's a 64kB write buffer and a 64kB read cache, that's 384kB. Plus all the other stuff, e.g. the onscreen contents, the input parsing (with a big ooops to gnome bug 730226 -- irrelevant now), all the internal structures and even the kitchen sink. Hence my wild guess of about half a meg of footprint per VTE.

summary: - Doesn't properly release VTE, causing FD leak
+ Doesn't properly release VTE, causing mem and FD leak
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.