You will already need to handle the case when you don't get the last request, because there are more ways the user can just vanish on you: suspending the computer, killing the browser process, crashing the browser, turning off WiFi or going out of range, etc, etc.
The best you can hope for is a "happy path" where you can clear up a little sooner in most cases, but you can't rely on it.
If a user being on a webpage is holding open a resource, that might be an architectural issue.
That definitely is an architectural issue. Unfortunately it is essentially a cache management matter (cache invalidation considers when something becomes useless/wasteful, as well as when it becomes incorrect) which is a hard problem to solve.
There are legitimate circumstances where a user causes a long running prices to start, that it is wasteful to continue if incomplete when they vanish (because the result isn't going to be useful to another user, or even that same user if they return).
While you can't catch everything (the user's machine losing power or all connectivity, etc.) you can catch a deliberate exit. And in the case of a long running process, if the user's exit was accidental it is more likely that they'll be back and having continued the process is useful.
The best you can hope for is a "happy path" where you can clear up a little sooner in most cases, but you can't rely on it.
If a user being on a webpage is holding open a resource, that might be an architectural issue.