# The FreeBSD thread that tried to close a vnode it already held

A process on FreeBSD 15.1 kept spending CPU time in the kernel after it was
asked to exit. Its stack led from a file descriptor lookup through fdescfs
straight into the final close of the same open file. A trace and a live dump
showed that the thread requesting the vnode lock already owned it. A later
run on stock 15.1-RELEASE-p6 reproduced the stack and the stuck process. The
proposed fix changes when the lookup releases its file reference, and still
needs validation.

## 1. A close showed up inside a lookup

The initial incident involved a worker in `dust`, a disk-usage tool. Repeated
stack samples showed it in the vnode lock retry path. A separate thread was
waiting for process exit to finish. Across two samples roughly four seconds
apart, the stuck worker accumulated another 4.055 seconds of system CPU time.
A stack can tell us which lock acquisition a thread reached. To explain the
hang, we also needed to know who owned that lock.
[Incident trace and repeated stack samples](../vfs-hang/dust-6417-analysis.md#24-pid-71963-dump-independently-confirms-the-callback-state-and-pathname)

A file descriptor is a number in a process's descriptor table. It refers to a
kernel file object, which in this case holds a vnode, the kernel's
representation of the underlying ZFS directory. With the `nodup` mount
option, fdescfs can resolve a lookup directly to that underlying vnode.
Looking up an object and closing an open file are distinct operations from
user space. Inside the kernel, releasing the lookup's temporary file
reference can turn one into the other.
[Lookup and close paths](ROOT-CAUSE.md#2-the-failing-sequence)

## 2. The trace followed one lifetime of the file object

Kernel addresses can be reused. The trace included earlier appearances of
the same file address associated with other vnodes, so the investigation
followed only the lifetime in which the file and vnode matched all the way
through the stall.

**Annotated reconstruction from selected trace events.** The rows preserve
the observed operation order. The reference counts are point samples.

| Thread | Observed operation |
| --- | --- |
| Lookup worker | Entered the fdescfs callback and began acquiring the target vnode lock. |
| Closing thread | Removed the descriptor and completed its file close while the lookup's acquisition was in progress. |
| Lookup worker | Acquired the vnode exclusively and entered the final file drop. |
| Lookup worker | Entered `vn_close1()` with the same vnode still locked. The lock word still identified this worker in a later stalled sample. |

[Selected release-ordering events](../vfs-hang/evidence/race.Mm7nhm-release-excerpt.txt)

**Correction.** An earlier trace filter compared the CTF-decoded vnode type
with the directory enum value. The two views of the same field explained why
the expected events were missing. The enum view read 258 and the packed type
byte read 2, the directory value. Reading the packed byte recovered the
events used above.
[Trace filter correction and dump analysis](../vfs-hang/dust-6417-analysis.md)

## 3. The dump identified the owner

**Observation from the p1 live dump.** The file's vnode, the callback's
output vnode, and the vnode from the trace all matched. The lock owner
matched the stalled worker. The file reference count was zero, and the
callback's `fdropped` flag was still clear. The dump was interpreted using
CTF layouts and symbol tables from archived binaries whose hashes matched
the incident artifacts.
[Dump inspection and binary identities](../vfs-hang/dust-6417-analysis.md#24-pid-71963-dump-independently-confirms-the-callback-state-and-pathname)

**Source interpretation.** In the `nodup` branch, `fdesc_get_ino_alloc()`
acquires the target vnode through `vget()` and subsequently releases the file
through `fdrop()`. The lookup's reference keeps the file object alive after
another thread removes its descriptor. When the lookup releases the last
reference, final close runs in the lookup thread and asks for a conflicting
lock on the same vnode. That thread can release its original lock only once
the close call returns, and the close remains in the lock retry path. The
zero count and clear flag fit a final drop that has started and has yet to
return.
[Source and callback interpretation](ROOT-CAUSE.md#2-the-failing-sequence)

## 4. A stock guest reproduced the same stack

**Correction.** The first probe used the fdescfs root itself as the target.
The incident dump established that the lookup root and the open ZFS
directory were different objects. The revised test used distinct objects
and checked their identity through the mounted interface before attempting
to reproduce the hang. Earlier timing-only attempts had completed without
the incident stack.
[Probe corrections](ROOT-CAUSE.md#5-corrections-from-failed-attempts)

A disposable stock 15.1-RELEASE-p6 guest reproduced the hang on its first
recorded attempt in that configuration. The guest emitted the same chain
from lookup to final close, reported 45.098922 seconds of system CPU time
at capture, and still showed the process after `SIGKILL`.

**Selected guest stack, line-wrapped for readability.** Read from bottom to
top to follow the calls.

```text
_vn_lock_fallback+0x9e vn_close1+0x1b0 vn_closefile+0x3d _fdrop+0x11
fdesc_get_ino_alloc+0x91 vn_vget_ino_gen+0x16b fdesc_lookup+0x145
vfs_lookup+0x401 namei+0x26d kern_statat+0xfe sys_fstatat+0x27
```

The guest established that the process hang persisted on p6. The direct
lock-owner evidence came from the p1 incident dump. The guest trial did
not measure how often the race occurs in normal use, and a whole-system
hang reported separately remains unverified.
[Guest console](results/15.1/run.fq85Wo/console.log#L168) ·
[Guest build metadata](results/15.1/run.fq85Wo/results.json)

## 5. The fix needs to preserve vnode lifetime

Moving the file drop earlier raises a lifetime question. The lookup still
needs the vnode after releasing the file object that held it. The candidate
change would acquire an independent vnode reference, release the file
while the target vnode is unlocked, and then complete vnode acquisition.
That gives final close room to take the lock while keeping the lookup's
target alive. A patch still needs to handle failed acquisition and the
case where the vnode becomes unusable during the handoff.

Validation would compare an unpatched and patched kernel under the same
ordering. The baseline should reach the recorded conflict. The patched
run should complete final close and then finish lookup or return a normal
error if the target is no longer usable. The repository contains a draft
for private FreeBSD triage. A patch and that comparison are still pending.
[Validation plan](PLAN.md#5-validate-and-report) ·
[Local report draft](BUG.md)
