Bug 98891

Summary: crashes when regenerating pdf
Product: [Unmaintained] kpdf Reporter: Alejandro Exojo <suy>
Component: generalAssignee: Albert Astals Cid <aacid>
Status: RESOLVED FIXED    
Severity: crash CC: gschintgen, jens-bugs.kde.org, olli
Priority: NOR    
Version: 0.4   
Target Milestone: ---   
Platform: unspecified   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Alejandro Exojo 2005-02-09 00:35:17 UTC
Version:           0.4 (using KDE 3.3.92 (beta2), compiled sources)
Compiler:          gcc version 3.3.5 (Debian 1:3.3.5-8)
OS:                Linux (i686) release 2.6.7-1-386

Hi.

I'm experiencing a crash very often. It happens when KPDF refreshes (BTW, through the great "watch file" feature) a file that I'm creating with docbook/jade. The problem seems to be, that it starts to load it when jade is still working (so, the PDF is still being generated).

This is what KPDF outputs to a console. Note that the crash happens _before_ the new PDF file is completely generated:

kdecore (KConfigSkeleton): Creating KConfigSkeleton (0x8175ec8)
kdecore (KConfigSkeleton): KConfigSkeleton::readConfig()
kio (KDirWatch): Can't use FAM (fam daemon not running?)
kio (KDirWatch): Available methods: Stat
kparts: MainWindow::createGUI, part=0x81517f0 KPDF::Part unnamed
kio (KSycoca): Trying to open ksycoca from /var/tmp/kdecache-alex/ksycoca
kio (KDirWatch): Added File /home/alex/pfc/kde/mdesigner/doc/misc/memoria.pdf [KDirWatch-1]
kio (KDirWatch): Global Poll Freq is now 500 msec
kio (KDirWatch):  Started Polling Timer, freq 500
kio (KDirWatch):  Setup Stat (freq 500) for /home/alex/pfc/kde/mdesigner/doc/misc/memoria.pdf
kio (KDirWatch): KDirWatch-1 emitting dirty /home/alex/pfc/kde/mdesigner/doc/misc/memoria.pdf
kpdf: Error (0): PDF file is damaged - attempting to reconstruct xref table...
kpdf: Error: Couldn't find trailer dictionary
kpdf: Error: Couldn't read xref table
kio (KDirWatch): KDirWatch-1 emitting dirty /home/alex/pfc/kde/mdesigner/doc/misc/memoria.pdf
kio (KDirWatch): KDirWatch-1 emitting dirty /home/alex/pfc/kde/mdesigner/doc/misc/memoria.pdf
kpdf: Error (0): PDF file is damaged - attempting to reconstruct xref table...
kpdf: Error: Couldn't find trailer dictionary
kpdf: Error: Couldn't read xref table
kio (KDirWatch): KDirWatch-1 emitting dirty /home/alex/pfc/kde/mdesigner/doc/misc/memoria.pdf
kpdf: Error (0): PDF file is damaged - attempting to reconstruct xref table...
kpdf: Error: Couldn't find trailer dictionary
kpdf: Error: Couldn't read xref table
kio (KDirWatch): KDirWatch-1 emitting dirty /home/alex/pfc/kde/mdesigner/doc/misc/memoria.pdf
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kio (KDirWatch): KDirWatch-1 emitting dirty /home/alex/pfc/kde/mdesigner/doc/misc/memoria.pdf
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error: font resource is not a dictionary
kpdf: Error (0): PDF file is damaged - attempting to reconstruct xref table...
kpdf: Error: Couldn't find trailer dictionary
kpdf: Error: Couldn't read xref table
KCrash: crashing... crashRecursionCounter = 2
KCrash: Application Name = kpdf path = <unknown> pid = 25199

The crash dialog showed this backtrace:

Using host libthread_db library "/lib/tls/libthread_db.so.1".
`system-supplied DSO at 0xffffe000' has disappeared; keeping its symbols.
[Thread debugging using libthread_db enabled]
[New Thread 1096481920 (LWP 25199)]
[KCrash handler]
#3  0x418314d3 in KPDFDocument::requestDone ()
   from /home/alex/build/lib/kde3/libkpdfpart.so
#4  0x4183d7af in PDFGenerator::customEvent ()
   from /home/alex/build/lib/kde3/libkpdfpart.so
#5  0x40c96dbc in QObject::event () from /usr/share/qt3/lib/libqt-mt.so.3
#6  0x40c3cbff in QApplication::internalNotify ()
   from /usr/share/qt3/lib/libqt-mt.so.3
#7  0x40c3c1fe in QApplication::notify () from /usr/share/qt3/lib/libqt-mt.so.3
#8  0x407a3cd5 in KApplication::notify ()
   from /home/alex/build/lib/libkdecore.so.4
#9  0x40c3d9f6 in QApplication::sendPostedEvents ()
   from /usr/share/qt3/lib/libqt-mt.so.3
#10 0x40c3d876 in QApplication::sendPostedEvents ()
   from /usr/share/qt3/lib/libqt-mt.so.3
#11 0x40be613d in QEventLoop::processEvents ()
   from /usr/share/qt3/lib/libqt-mt.so.3
#12 0x40c4ef58 in QEventLoop::enterLoop ()
   from /usr/share/qt3/lib/libqt-mt.so.3
#13 0x40c4ee08 in QEventLoop::exec () from /usr/share/qt3/lib/libqt-mt.so.3
#14 0x40c3ce51 in QApplication::exec () from /usr/share/qt3/lib/libqt-mt.so.3
#15 0x0804ef8e in main ()


This doesn't happens always, but very often. It seems the problem is KDirWatch, because it says that the file is modified, but not that _still_ is being modified.

I'm running kdelibs and kdegraphics from a recent HEAD (2 or 3 days old).

Thanks in advance.
Comment 1 Albert Astals Cid 2005-02-09 11:36:07 UTC
This is indeed a problem on how modified files are handled by KDirWatch + KPDF. What we currently do is attach a KDirWatch to the current file, if the file gets modified and is not modified again in the next 750 ms assume it has stoped beign modified and open it. The problem is that probably when generation begins it sets the size of the file to 0 and does not changes the file until all the generation is finished and that generation takes more than 750 ms. 

Can you verify that the behaviour is that: size of the file to 0 at the begining and does not change until the generation has finished.
Comment 2 Alejandro Exojo 2005-02-09 13:45:02 UTC
El Miércoles, 9 de Febrero de 2005 11:36, Albert Astals Cid escribió:
> Can you verify that the behaviour is that: size of the file to 0 at the
> begining and does not change until the generation has finished.

No, I'm sorry, but it isn't. :-(

During the generation, its size is never zero. It grows and shrinks several 
times, but never is zero.

Fell free to tell me if you need more info. ;)

Comment 3 Albert Astals Cid 2005-02-12 00:40:54 UTC
Strange, i can get it to say 

kpdf: Error (0): PDF file is damaged - attempting to reconstruct xref table... 
kpdf: Error: Couldn't find trailer dictionary 
kpdf: Error: Couldn't read xref table 

But it never crashes here and reopens the document when it is correct.
Comment 4 Albert Astals Cid 2005-04-01 12:08:43 UTC
Changing the summary to something more meaningfull
Comment 5 Albert Astals Cid 2005-04-01 12:09:59 UTC
*** Bug 102987 has been marked as a duplicate of this bug. ***
Comment 6 Albert Astals Cid 2005-05-18 15:46:18 UTC
*** Bug 105885 has been marked as a duplicate of this bug. ***
Comment 7 Jens 2005-05-19 16:50:14 UTC
Hi,

my bug (Bug 105885) has been attached to this one as a duplicate, but I had a different crash: For me, KPDF repeatedly *always* crashes at QPixmap::ConvertFromImage. I'm using PDFlatext with Kile to create PDF documents with embedded PDF images.

Here's one backtrace (the other is at Bug 105885).

(no debugging symbols found)
Using host libthread_db library "/lib/tls/libthread_db.so.1".
[KCrash handler]
#3  0x40c504b3 in QPixmap::convertFromImage ()
   from /usr/lib/qt3/lib/libqt-mt.so.3
#4  0x40c506e7 in QPixmap::QPixmap () from /usr/lib/qt3/lib/libqt-mt.so.3
#5  0x417d146c in PDFGenerator::customEvent ()
   from /opt/kde3/lib/kde3/libkpdfpart.so
#6  0x40c3a49e in QObject::event () from /usr/lib/qt3/lib/libqt-mt.so.3
#7  0x40bd7d5f in QApplication::internalNotify ()
   from /usr/lib/qt3/lib/libqt-mt.so.3
#8  0x40bd9a83 in QApplication::notify () from /usr/lib/qt3/lib/libqt-mt.so.3
#9  0x408626b1 in KApplication::notify () from /opt/kde3/lib/libkdecore.so.4
#10 0x40bd8ed0 in QApplication::sendPostedEvents ()
   from /usr/lib/qt3/lib/libqt-mt.so.3
#11 0x40bd8f54 in QApplication::sendPostedEvents ()
   from /usr/lib/qt3/lib/libqt-mt.so.3
#12 0x40b84bc0 in QEventLoop::processEvents ()
   from /usr/lib/qt3/lib/libqt-mt.so.3
#13 0x40befe51 in QEventLoop::enterLoop () from /usr/lib/qt3/lib/libqt-mt.so.3
#14 0x40befc96 in QEventLoop::exec () from /usr/lib/qt3/lib/libqt-mt.so.3
#15 0x40bd994f in QApplication::exec () from /usr/lib/qt3/lib/libqt-mt.so.3
#16 0x08050987 in ?? ()
#17 0xbfffed10 in ?? ()
#18 0xbfffece0 in ?? ()
#19 0x00000001 in ?? ()
#20 0x00000000 in ?? ()
#21 0x03df6174 in ?? ()
#22 0x0804d1e6 in ?? ()
#23 0x40016ff4 in ?? () from /lib/ld-linux.so.2
#24 0x400176ac in ?? ()
#25 0x00000000 in ?? ()
#26 0xbfffed88 in ?? ()
#27 0x40007c5c in _dl_rtld_di_serinfo () from /lib/ld-linux.so.2
#28 0x41438e90 in __libc_start_main () from /lib/tls/libc.so.6
#29 0x0804f241 in ?? ()


Thanks!
Jens
Comment 8 Jens 2005-05-20 09:57:27 UTC
Additional information: Crashes happen more often when pdflatex is run twice (e.g. due to changing references within the LaTeX file).
Comment 9 Jens 2005-05-24 12:05:25 UTC
Another crash - currently KPDF crashes about every second PDFlatex run. :-(

(no debugging symbols found)
Using host libthread_db library "/lib/tls/libthread_db.so.1".
[KCrash handler]
#3  0x417d1476 in PDFGenerator::customEvent ()
   from /opt/kde3/lib/kde3/libkpdfpart.so
#4  0x40c3a49e in QObject::event () from /usr/lib/qt3/lib/libqt-mt.so.3
#5  0x40bd7d5f in QApplication::internalNotify ()
   from /usr/lib/qt3/lib/libqt-mt.so.3
#6  0x40bd9a83 in QApplication::notify () from /usr/lib/qt3/lib/libqt-mt.so.3
#7  0x408626b1 in KApplication::notify () from /opt/kde3/lib/libkdecore.so.4
#8  0x40bd8ed0 in QApplication::sendPostedEvents ()
   from /usr/lib/qt3/lib/libqt-mt.so.3
#9  0x40bd8f54 in QApplication::sendPostedEvents ()
   from /usr/lib/qt3/lib/libqt-mt.so.3
#10 0x40b84bc0 in QEventLoop::processEvents ()
   from /usr/lib/qt3/lib/libqt-mt.so.3
#11 0x40befe51 in QEventLoop::enterLoop () from /usr/lib/qt3/lib/libqt-mt.so.3
#12 0x40befc96 in QEventLoop::exec () from /usr/lib/qt3/lib/libqt-mt.so.3
#13 0x40bd994f in QApplication::exec () from /usr/lib/qt3/lib/libqt-mt.so.3
#14 0x08050987 in ?? ()
#15 0xbfffed10 in ?? ()
#16 0xbfffece0 in ?? ()
#17 0x00000001 in ?? ()
#18 0x00000000 in ?? ()
#19 0x03df6174 in ?? ()
#20 0x0804d1e6 in ?? ()
#21 0x40016ff4 in ?? () from /lib/ld-linux.so.2
#22 0x400176ac in ?? ()
#23 0x00000000 in ?? ()
#24 0xbfffed88 in ?? ()
#25 0x40007c5c in _dl_rtld_di_serinfo () from /lib/ld-linux.so.2
#26 0x41438e90 in __libc_start_main () from /lib/tls/libc.so.6
#27 0x0804f241 in ?? ()
Comment 10 Jens 2005-05-25 16:24:02 UTC
I would guess that the only real way to close this bug is to enable Kghostview / KPDF to check the file integrity _before_ trying to load it. I.e. check if the PDF / Postscript file is not corrupt. If this cannot be easily done, Kghostview/KPDF should check if the file is opened for writing(!) by another application, and if so, refuse to update it until closed.

If all that isn't feasible for KPDF: I don't know if searching for an "%%EOF" marker at the end of the file will do the trick, but this might avoid waiting for a fixed time (after all, loading a corrupt PDF or Postscript file can happen all the time, not just during re-creation by e.g. pdflatex).

I just tested this for PDF files: This simple script will test for an %%EOF marker in PDF files (my *GUESS* is that every PDF file has such a marker and that it is added last, before closing the file).

Bash script:

> while true; do echo -n $(date +%H:%M:%S) "  "; test "$(tail -n 1 Bericht.pdf)" == "%%EOF" && echo "EOF found" || echo; sleep 0.1; done 

will output:

16:19:54   EOF found
16:19:54   EOF found
16:19:54   EOF found
16:19:54   EOF found
16:19:54   EOF found
16:19:54   EOF found
16:19:54   EOF found
16:19:54   EOF found
16:19:54   EOF found
16:19:55   EOF found
16:19:55   EOF found
16:19:55
16:19:55
16:19:55
16:19:55
16:19:55
16:19:55
16:19:55
16:19:56
16:19:56
16:19:56
16:19:56
16:19:56
16:19:56
16:19:56
16:19:56
16:19:57
16:19:57
16:19:57
16:19:57
16:19:57
16:19:58
16:19:58
16:19:58
16:19:58   EOF found
16:19:58   EOF found
16:19:58   EOF found
16:19:59   EOF found
16:19:59   EOF found
16:19:59   EOF found


when PDFlatex runs over my PDF file. (18 page document with a couple PNG and PDF embedded images and lots of text, on a P4-1800MHz). So this _looks_ like it works.

Please implement something like this for KPDF and KGhostview. Thank you!


Jens
Comment 11 Willi Richert 2005-06-15 10:03:34 UTC
Another dump, after kpdf crashed while regenerating the pdf file twice shortly one after another with pdflatex:

Using host libthread_db library "/lib/tls/libthread_db.so.1".
[Thread debugging using libthread_db enabled]
[New Thread -1208132960 (LWP 11828)]
[New Thread -1215415376 (LWP 12504)]
[Thread debugging using libthread_db enabled]
[New Thread -1208132960 (LWP 11828)]
[New Thread -1215415376 (LWP 12504)]
[Thread debugging using libthread_db enabled]
[New Thread -1208132960 (LWP 11828)]
[New Thread -1215415376 (LWP 12504)]
0x009497a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#0  0x009497a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1  0x00a22a51 in ___newselect_nocancel () from /lib/tls/libc.so.6
#2  0x00aeef92 in _XEnq () from /usr/X11R6/lib/libX11.so.6
#3  0x00aef36e in _XRead () from /usr/X11R6/lib/libX11.so.6
#4  0x00af04cb in _XReply () from /usr/X11R6/lib/libX11.so.6
#5  0x00ae5aec in XQueryPointer () from /usr/X11R6/lib/libX11.so.6
#6  0x027b017f in QCursor::pos () from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#7  0xb7d0430d in PageView::notifyPageChanged ()
   from /usr/lib/kde3/libkpdfpart.so
#8  0xb7cef563 in KPDFDocument::requestDone ()
   from /usr/lib/kde3/libkpdfpart.so
#9  0xb7cfc1fb in PDFGenerator::customEvent ()
   from /usr/lib/kde3/libkpdfpart.so
#10 0x02864cde in QObject::event () from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#11 0x02808a19 in QApplication::internalNotify ()
   from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#12 0x02808baa in QApplication::notify ()
   from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#13 0x005423f8 in KApplication::notify () from /usr/lib/libkdecore.so.4
#14 0x02809b76 in QApplication::sendPostedEvents ()
   from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#15 0x02809c02 in QApplication::sendPostedEvents ()
   from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#16 0x027b6cd5 in QEventLoop::processEvents ()
   from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#17 0x0281e0b5 in QEventLoop::enterLoop ()
   from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#18 0x0281e00e in QEventLoop::exec () from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#19 0x02807c1b in QApplication::exec () from /usr/lib/qt-3.3/lib/libqt-mt.so.3
#20 0x0804ee0d in ?? ()
#21 0xbffff6c0 in ?? ()
#22 0x00000001 in ?? ()
#23 0x00000001 in ?? ()
#24 0x00000000 in ?? ()
Comment 12 Albert Astals Cid 2005-07-03 16:53:37 UTC
*** Bug 108495 has been marked as a duplicate of this bug. ***
Comment 13 Gilles Schintgen 2005-07-03 17:05:18 UTC
> *** Bug 108495 has been marked as a duplicate of this bug. ***

Oops, I'm sorry about that.
Comment 14 Albert Astals Cid 2005-07-03 20:26:31 UTC
Can anybody on this thread try this patch?

Index: PDFDoc.cc
===================================================================
--- PDFDoc.cc   (revisión: 431248)
+++ PDFDoc.cc   (copia de trabajo)
@@ -114,7 +114,27 @@

 GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
   str->reset();
-
+
+  char eof[8];
+  int pos = str->getPos();
+  str->setPos(7, -1);
+  eof[0] = str->getChar();
+  eof[1] = str->getChar();
+  eof[2] = str->getChar();
+  eof[3] = str->getChar();
+  eof[4] = str->getChar();
+  eof[5] = str->getChar();
+  eof[6] = str->getChar();
+  eof[7] = '\0';
+  if (strstr(eof, "%%EOF") == NULL)
+  {
+    error(-1, "Document does not has ending %%EOF");
+    errCode = errDamaged;
+    return gFalse;
+  }
+
+  str->setPos(pos);
+
   // check header
   checkHeader();
Comment 15 Albert Astals Cid 2005-07-04 23:45:52 UTC
SVN commit 431645 by aacid:

This may fix #98891 i can not tell because i've never been able to reproduce it. I'm committing it to trunk (kde 3.5) and the kde 3.4 branch (so it will be in kde 3.4.2) if you can still reproduce this problem when using one of this releases please reopen this bug.
BUGS: 98891


 M  +21 -1     PDFDoc.cc  


--- trunk/KDE/kdegraphics/kpdf/xpdf/xpdf/PDFDoc.cc #431644:431645
@@ -114,7 +114,27 @@
 
 GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
   str->reset();
-
+  
+  char eof[8];
+  int pos = str->getPos();
+  str->setPos(7, -1);
+  eof[0] = str->getChar();
+  eof[1] = str->getChar();
+  eof[2] = str->getChar();
+  eof[3] = str->getChar();
+  eof[4] = str->getChar();
+  eof[5] = str->getChar();
+  eof[6] = str->getChar();
+  eof[7] = '\0';
+  if (strstr(eof, "%%EOF") == NULL)
+  {
+    error(-1, "Document does not has ending %%EOF");	      
+    errCode = errDamaged;
+    return gFalse;
+  }
+  
+  str->setPos(pos);
+  
   // check header
   checkHeader();