Dear Claudio and Gustaf,
being this a legacy script, running also on Aolserver, I believe that the delete operations called after
"ns_writer submitfile $path"
are implemented via "ns_unlink".
But calling "ns_unlink" on Naviserver 4.99.22 generates the exception/message about the function being deprecated.
It is this exception/message that clashes with "ns_writer" and causes the error "connection socket is detached".
Replacing "ns_unlink" with "file delete" should avoid the exception/message about the function being deprecated and therefore should make the error "connection socket is detached" disappear.
Hope it helps,
Maurizio