[Tex/LaTex] xelatex: I can’t write on file `test.pdf’.

Ubuntuxetex

Every so often I'll be working and all of a sudden xelatex won't work, saying "I can't write on file `test.pdf'". But why not? Is it something I did? Did I insult xelatex? Now it won't even compile my test.tex.

This is XeTeX, Version 3.1415926-2.2-0.9995.2 (TeX Live 2009/Debian) (format=xelatex 2011.4.22). Ubuntu 10.04.

The input

\documentclass{article}
\usepackage{fontspec,xltxtra,xunicode}
\setmainfont{Inconsolata}
\begin{document}
hello world
\end{document}

The error

LaTeX Font Info:    Overwriting math alphabet `\mathsf' in version `bold'
(Font)                  OT1/cmss/bx/n --> EU1/lmss/bx/n on input line 4.
LaTeX Font Info:    Overwriting math alphabet `\mathtt' in version `bold'
(Font)                  OT1/cmtt/m/n --> EU1/lmtt/bx/n on input line 4.
 [1
! I can't write on file `test.pdf'.
(Press Enter to retry, or Control-D to exit; default file extension is `.pdf')
Please type another file name for output: 
! Emergency stop.
<to be read again> 
                   \endgroup \set@typeset@protect 
l.6 \end{document}

End of file on the terminal!


Here is how much of TeX's memory you used:
 5003 strings out of 495724
 81422 string characters out of 1189346
 150108 words of memory out of 3000000
 8144 multiletter control sequences out of 15000+50000
 3664 words of font info for 17 fonts, out of 3000000 for 9000
 28 hyphenation exceptions out of 8191
 40i,4n,27p,455b,147s stack positions out of 5000i,500n,10000p,200000b,50000s
No pages of output.

The inconsistency of it all

Ten minutes later (now), after tinkering around, it works. So I delete the contents of the file, and paste what I posted here, and, it works. Then I delete test.* except for the tex file, try xelatex again, and it doesn't work. The tinkering was to change the tex file to this:

\documentclass[12pt,letterpaper]{article}

 \usepackage{fontspec}% provides font selecting commands
 \usepackage{xunicode}% provides unicode character macros
 \usepackage{xltxtra} % provides some fixes/extras

 \setromanfont[Mapping=tex-text]{Inconsolata}
\begin{document}
hello world
\end{document}

strace -f

Strace shows a memory error right before the "cannot create pdf" line. I'm experimenting with adding more swap. Currently my system is down its last 300-400 MiB of ram, and I'm seeing xelatex using upwards of 200 MiB. Which means a couple more browser tabs or another pdf window could foul things up..

clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7887738) = -1 ENOMEM (Cannot allocate memory)`.

Best Answer

! I can't write on file `skills.pdf'.

In this case, the error is caused by not having enough memory. The clone(...) = -1 ENOMEM (Cannot allocate memory) error from strace prompted me to check my ram usage. Running xelatex with 150M of free ram provokes the write/clone() error. Running with at least 500M of free ram consistently avoids the error. I haven't experimented enough to know exactly how much free ram is enough, but I suppose that also depends on the complexity of the tex file.

To compensate, I created a swap file (fwiw, I usually don't have one).

dd if=/dev/zero of=/var/512mb.swap bs=1M count=512
mkswap /var/512mb.swap
swapon /var/512mb.swap