Is there a specific reason why browsers don't inquire whether users want to close a tab or not? This feature is enabled in some browsers (possibly all, though not certain), but only for multiple tabs, and users must permit this, with varying levels of difficulty. For instance, enabling this feature is relatively simple in Firefox but almost impossible in Chrome unless one does a search on how to enable it. However, this still applies solely to multiple tabs and only when shutting down the browser. If a user opts to close several tabs at once without closing the browser (for example, by choosing "close all tabs to the right"), there won't be a warning either.
So I wonder if there's a specific reason (whether technological, performance-related, usability-based, etc.) for which all browsers prevent user's locus of control.
In my opinion, and according to all accepted heuristics, this should be a feature. In any case, an optional one, but it should be present, so it's weird to find out it doesn't even exist as a possibility.
Very nice and interesting answers so far, but they seem to skirt around two particular facts: the potential for information recovery and the annoyance of accidental tab closure. Regarding the annoyance, I agree that it's frustrating, which is why I emphasized that any solution should be optional.
As for the possibility of recovering information, this isn't always the case. To begin with, many apps are poorly coded, making recovery difficult or impossible. But let me illustrate with a real-life experience that prompted me to ask this question.
I had a lengthy process running in one tab, which didn't focus. The only requirement was not to close the window while it was running. I continued working on something else in another tab, and when I tried to return to the initial tab, I accidentally closed it (a situation similar to what Angel mentioned).
In my case, it was a Psycho-Py experiment, but my curiosity led me to test other occurrences that weren't forms, but running processes: a WordPress backup using the All in One Security plugin, a WordPress batch image conversion plugin, and a PHP upgrade on a server. I repeated the process by closing the tab with the running process while in another tab. All of these instances allowed me to close the tab without warning, resulting in complete loss, with no possibility of recovery. In one extreme case, I even had to restart the server.
I believe browsers should offer an option to ask for confirmation when closing a tab with an ongoing process, allowing users to activate or deactivate this feature as needed. Users should ALWAYS have complete control. For instance, when I close my Mac, it prompts me to confirm what to do with any important open app that is running. It ignores some apps (such as Spotify or Netflix) but doesn't ignore others (like Firefox or Time controlled tasks like Togglr) if there's a process running or an unsaved form. This selective approach puts control where it should be - in the hands of the user.