Long table inserting multiple table headers on same page

Sometimes on a longtable I get broken output. It will insert the non-first table header at the top of the page. It's very rare and only seems to happen when there's a very specific amount of space remaining on the previous page.

Any ideas on what causes this output and a work-around?

And the document. It's a simplest reproduction I could manage to make. It's not just \needspace that causes it though. The same issue pops up on a longtable immediately after another longtable, and very rarely after a paragraph.

\documentclass[10pt,a4paper,final,twoside]{article}
\usepackage[a4paper,landscape]{geometry}
\usepackage{longtable}
\usepackage{needspace}
\begin{document}
xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz\\[30pt]
\needspace{10\baselineskip}
\begin{longtable}{l l l l l}
\multicolumn{5}{l}{Data (continued)}\\*
\textbf{Addr} & \textbf{Type} & \textbf{Product} & \textbf{Serial} & \textbf{Location} \\*
\hline
\multicolumn{5}{l}{Data}\\*
\textbf{Addr} & \textbf{Type} & \textbf{Product} & \textbf{Serial} & \textbf{Location} \\*
\hline
25 & Data & Data & Data & Data \\
26 & Data & Data & Data & Data \\
27 & Data & Data & Data & Data \\
28 & Data & Data & Data & Data \\
29 & Data & Data & Data & Data \\
30 & Data & Data & Data & Data \\
31 & Data & Data & Data & Data \\
32 & Data & Data & Data & Data \\
\end{longtable}

\end{document}


I've tested this in pdflatex, xelatex and lualatex on both debian wheezy texlive and miktex, always gives the same bad output.

Longtable is in a bit of a delicate state as it switches output routines at the start so using \needspace there is a bu it brave. However your example can be simplified to

\documentclass[10pt,a4paper,final,twoside,landscape]{article}

\setlength\textwidth{591.5302pt}
\setlength\textheight{418.25368pt}

\usepackage{longtable}

\begin{document}
xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz

xyz\\[30pt]

%%\clearpage
\begin{longtable}{l l l l l}
\multicolumn{5}{l}{Data (continued)}\\*
\textbf{Addr} & \textbf{Type} & \textbf{Product} & \textbf{Serial} & \textbf{Location} \\*
\hline
\multicolumn{5}{l}{Data}\\*
\textbf{Addr} & \textbf{Type} & \textbf{Product} & \textbf{Serial} & \textbf{Location} \\*
\hline
25 & Data & Data & Data & Data \\
26 & Data & Data & Data & Data \\
27 & Data & Data & Data & Data \\
28 & Data & Data & Data & Data \\
29 & Data & Data & Data & Data \\
30 & Data & Data & Data & Data \\
31 & Data & Data & Data & Data \\
32 & Data & Data & Data & Data \\
\end{longtable}

\end{document}


Which shows the same problem, which is unfortunate as it uses no contributed packages so limits the number of people who could be blamed.

Uncommenting the \clearpage is the usual workaround.

Meanwhile: comforting thought from egreg

Thanks for the clear test case. I'll look later