diff --git a/src/code/filesys.lisp b/src/code/filesys.lisp index 482b34b..aca5143 100644 --- a/src/code/filesys.lisp +++ b/src/code/filesys.lisp @@ -475,13 +475,15 @@ or if PATHSPEC is a wild pathname." (let* ((truename (probe-file file)) (namestring (when truename (native-namestring truename :as-file t)))) - (when (streamp file) - (close file :abort t)) (unless namestring (error 'simple-file-error :pathname file :format-control "~S doesn't exist." :format-arguments (list file))) + (when (streamp file) + (close file :abort t) + (unless (probe-file namestring) ; close could already delete file + (return-from delete-file t))) (multiple-value-bind (res err) (sb!unix:unix-unlink namestring) (unless res (simple-file-perror "couldn't delete ~A" namestring err)))) diff --git a/tests/stream.impure.lisp b/tests/stream.impure.lisp index 3540257..277006f 100644 --- a/tests/stream.impure.lisp +++ b/tests/stream.impure.lisp @@ -578,4 +578,10 @@ (read-sequence buffer s)))) (delete-file pathname)) +(with-test (:name :bug-406271) + (let ((pathname "delete-file-deletes-twice")) + (with-open-file (stream pathname :direction :output :if-does-not-exist :create) + (delete-file stream)) + (assert (not (probe-file pathname))))) + ;;; success