FileNode with onDestroyRemoveChildren deletes a file that the same call moves out of the folder

Issue Description

A client sends one FileNode/set that moves a file from folder A to folder B and destroys A with onDestroyRemoveChildren set to true. A sync client does this when it replays “mv A/keep.txt B/” followed by “rm -r A” as one batch. On the v1.0.0 branch the move is rejected with willDestroy and the file is destroyed together with A. Its content is gone too: downloading the file’s blobId returns 404 right after the call.

With onDestroyRemoveChildren set to false the server does the opposite. Moving every child of A to B and destroying A in the same call works, and the files are kept. So with the flag false the moved files no longer count as children of A, and with the flag true they are deleted as children of A.

Expected Behavior

The moved file ends up in B, and only what is still in A at the end of the call is destroyed.

Actual Behavior

A = ur, B = uu, keep.txt = us, drop.txt = ut:

["FileNode/set", {"accountId": "w", "update": {"us": {"parentId": "uu"}},
                  "destroy": ["ur"], "onDestroyRemoveChildren": true}, "0"]
<- {"oldState": "sv2aq", "newState": "swaaq", "destroyed": ["ut", "us", "ur"],
    "notUpdated": {"us": {"type": "willDestroy", "description": "ID will be destroyed."}}}

["FileNode/get", {"accountId": "w", "ids": ["us", "ut", "ur"]}, "0"]
<- "list": [], "notFound": ["us", "ut", "ur"]

GET download URL for keep.txt's blobId (200 before the call) -> HTTP 404

For comparison, the same kind of call with the flag false (folder C = uv holding c1.txt = uw and c2.txt = ux) keeps both files:

["FileNode/set", {"accountId": "w", "update": {"uw": {"parentId": "uu"}, "ux": {"parentId": "uu"}},
                  "destroy": ["uv"], "onDestroyRemoveChildren": false}, "0"]
<- "updated": {"uw": {...}, "ux": {...}}, "destroyed": ["uv"]

Reproduction Steps

  1. Upload a small blob and create a folder with A and B in it, and keep.txt and drop.txt inside A.
  2. In one FileNode/set, move keep.txt to B and destroy A with onDestroyRemoveChildren true.
  3. Call FileNode/get for keep.txt. It is notFound, and its blob download returns 404.
1. ["FileNode/set", {"accountId": "<acc>", "create": {
     "t": {"name": "test", "parentId": null},
     "a": {"name": "A", "parentId": "#t"},
     "b": {"name": "B", "parentId": "#t"},
     "k": {"name": "keep.txt", "parentId": "#a", "blobId": "<blob>"},
     "d": {"name": "drop.txt", "parentId": "#a", "blobId": "<blob>"}}}, "0"]

2. ["FileNode/set", {"accountId": "<acc>", "update": {"<keep>": {"parentId": "<B>"}},
                     "destroy": ["<A>"], "onDestroyRemoveChildren": true}, "0"]

3. ["FileNode/get", {"accountId": "<acc>", "ids": ["<keep>"]}, "0"]

Stalwart Version

v0.16.x

Installation Method

Built from source

Database Backend

RocksDB

Blob Storage

RocksDB

Search Engine

Internal

Directory Backend

Internal

Additional Context

On the 1.0 deleted branch

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

This matches what the code does, confirming it’s a real bug rather than intended behavior.

In file_node_set (crates/jmap/src/file/set.rs), the “will destroy” set is computed once up front, before any updates are processed:

let mut will_destroy = response.collect_will_destroy(request.unwrap_destroy());

collect_will_destroy expands each explicitly-destroyed id into the whole subtree (everything currently parented under it in the resource cache snapshot taken at the start of the call). Then, when processing update, any id present in that pre-computed will_destroy set is rejected with willDestroy and left untouched for the update:

if will_destroy.contains(&id) || implicit_destroys.contains(&id.document_id()) {
    response.not_updated.append(id, SetError::will_destroy());
    continue 'update;
}

Because the subtree snapshot is taken before the move is applied, keep.txt is still considered a child of A at that point, so its parentId update to B is rejected as willDestroy — but the actual destroy pass still walks A’s children from the same stale snapshot and deletes it anyway (including its blob), rather than re-checking membership after updates are applied. With onDestroyRemoveChildren: false, the destroy path takes a different branch that doesn’t eagerly remove the moved children, which is why that case behaves correctly.

So the root cause is that the “will destroy” subtree computation and the final destroy pass both use the pre-update parent-child snapshot instead of re-evaluating folder membership after the in-batch updates are applied, causing moved items to be both rejected as updates and still physically deleted (with blob loss).

This is confirmed in the current source and looks like a genuine ordering/logic bug in the FileNode set handler. A human maintainer will need to follow up on a fix.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

Fixed in v1.0.0 (yet to be released).