Emacs on Windows 8: write-error, bad file descriptor

968 views Asked by At

I just installed Emacs on Win8. However, sometimes, when I save buffer to the file, emacs will just say:

Write error: bad file descriptor, c:/Users/...

I tried different version of emacs, no help. If I restart the computer, the problem will be solved for a short time. Then is occurs again.

Any ideas ?

update

I run the command toggle-debug-on-error, it gives me the following information:

Debugger entered--Lisp error: (file-error "Write error" "bad file descriptor" "c:/cygwin64/home/t-xins/workspace/codesnippet/html/main.js")
write-region(nil nil "c:/cygwin64/home/t-xins/workspace/codesnippet/html/main.js" nil t "c:/cygwin64/home/t-xins/workspace/codesnippet/html/main.js")
basic-save-buffer-2()
basic-save-buffer-1()
basic-save-buffer()
save-buffer(1)
call-interactively(save-buffer nil nil)
command-execute(save-buffer)

I observed that when my computer get awake after sleeping, the problem happens.

2

There are 2 answers

0
tanzoniteblack On

Things you might try to give us more information to help with this:

Run the function toggle-debug-on-error so that you can get a stack trace of the error next time it happens, which might give a clue as to what is happening.

It is possible, but I'm not very sure of it, that it could be an encoding issue, in which case the following lines might fix it:

(setenv "LANG" "en_US.UTF-8")
(set-language-environment 'utf-8)
(set-default-coding-systems 'utf-8)
(setq locale-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
0
BerndSchmitt On

A workaround for one case of this error. In my case, after saving once, a buffer could not be saved again, throwing the error message mentioned above. Saving to a different name and renaming this file did it. Not nice - but working. So, maybe someone finds this useful: :

(defun save-force (&optional override-local-key)
    "force saving a file (ignores bad-file-descriptor)"
    (interactive "P")
    (when override-local-key
        (local-set-key (kbd "C-x C-s") 'save-force)
        (print "C-x C-s overridden with 'save-force") )
    (let* ((buf-fname (buffer-file-name))
           (buf-fname1 (format "%s-temp%06d" buf-fname (random 1000000))) )
        (ignore-errors
            (write-region nil nil buf-fname1))
        (when (file-exists-p buf-fname1)
            (when (file-exists-p buf-fname)
                (delete-file buf-fname))
            (rename-file buf-fname1 buf-fname)
            (set-buffer-modified-p nil)
            (revert-buffer nil t) )))