[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: synchronization problem in libkhtml while doing a form submit
From:       B S Prabhakar <bsprab () yahoo-inc ! com>
Date:       2004-07-19 4:28:02
Message-ID: 40FB4B02.6040207 () yahoo-inc ! com
[Download RAW message or body]


why we should call KDE library only from main thread ? Is there any 
dependency in KDE library on main thread ?

We have a requirement where in the KHTML related operation is to be done 
in a plugin which is called by thirdparty application. here we don't 
have access to main thread. How to proceed in this situation with KDE 
library.

We are running event loop in one worker thread and KHTML related in 
other worker threads (Multiple threads) . Is this mode of working not 
supported / adviceable with KDE ?

Our code just calls openURI in a loop.
We are facing frequent crash in our application,


Attached is the sample which we used to test KHTMLPart openURI by 
multiple (2 worker threads) threads which crashed frequently

Most of the time crash happened after a while since program start (stack 
dump for the same is attached)

but some times application crashed soon after starting while executing 
connect API


****************************************************************************************************


#include "kjscript.h"

#include <qthread.h>
#include <kurl.h>
#include <khtml_part.h>
#include <dom/dom_node.h>
#include <kapp.h>
#include <qvariant.h>
#include <qlabel.h>
#include <qstring.h>
#include <pthread.h>


using namespace DOM;

static bool g_initialized = false;
int argc;
char * argv[1];
//static KApplication a(NULL,argc, argv, "p4" ,false,false);
KJScript::KJScript():QObject()
{


this->part = new KHTMLPart();
this->m_jscript = false;

// set defaults
this->part->setJScriptEnabled(1);
this->part->setJavaEnabled(1);
this->part->setPluginsEnabled(1);
this->part->setAutoloadImages(1);


// PAGE URL Request
QObject::connect(this->part->browserExtension(), 
SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)), 
this, SLOT(SlotOpenURL(const KURL &)));

// KHTMLPart::FormNotification formNfn=KHTMLPart::Before;
KHTMLPart::FormNotification formNfn=KHTMLPart::Only;
this->part->setFormNotification(formNfn);

QObject::connect( this->part, SIGNAL( completed() ), this, SLOT( 
documentCompleted() ) );

QObject::connect( this->part, SIGNAL(formSubmitNotification(const char 
*, const QString& , const QByteArray& , const QString& , const QString& 
, const QString& )),this,SLOT(formSubmit(const char *, const QString& , 
const QByteArray& , const QString&, const QString&, const QString&)) );

QObject::connect(this,SIGNAL(formSubmitsignal()),this,SLOT(handleForm()) );

}

KJScript::~KJScript()
{
if (this->part)
delete this->part;
}


DOM::HTMLDocument KJScript::getHtmlDocument(void)
{
if(this->part)
return this->part->htmlDocument();
}






void KJScript::documentCompleted(void)
{
cout << "Document completed without pending action"<<endl;
this->m_waitcond.wakeOne();


}

void KJScript::documentCompleted(bool completed)
{
cout << "Document completed ==>"<<completed <<endl;
fflush(stdout);

cout <<"About to send wakeOne signal"<<endl;
this->m_waitcond.wakeAll();


}




int KJScript::wait(unsigned long time) {
cout <<"Started waiting "<<endl;
fflush(stdout);
this->m_waitcond.wait(time);
}


void KJScript::openURL (const KURL &url )
{
cout <<"Opening url =="<< url.url()<<endl;
this->part->openURL(url);
}



class Worker: public QThread
{
public:
Worker()
{
QThread::start();
}

protected:
void run()
{
// a.exec();
cout <<"Event thread exiting"<<endl;
}
};

void KJScript::Init(void)
{
if(!g_initialized)
{
g_initialized = true;
Worker *eventThread = new Worker();

}
int argc;
char *argv[1];
}

void fetchPage (const char * file)
{
FILE *fp = NULL;
fp = fopen(file,"r"); // file with list of URLs


cout <<" current thread id is "<<(unsigned long) pthread_self()<<endl;

char url[200];
if(!fp)
return ;

while(fgets(url,200,fp))
{
url[strlen(url)-1] = '\0';
KJScript * jscript = new KJScript();

jscript->openURL(url);
jscript->wait(ULONG_MAX);

delete jscript;

}

}

class Fetcher : public QThread
{
QString m_filename;
public:
Fetcher(QString file)
{
m_filename = file;
QThread::start();
}
protected:
void run()
{
fetchPage(m_filename.data());
cout <<"Exiting thread Fetcher "<< m_filename <<endl;
}
};


void main(int argc, char*argv[])
{
KApplication a(NULL,argc, argv, "p4" ,false,false);

Fetcher *f1 = new Fetcher("form_urls.txt");
Fetcher *f2 = new Fetcher("new_urls.txt");

a.exec();

cout <<" End of the application "<<endl;
}

**********************************************************************************************

stack dump at crash

#########################################################################

Program received signal SIGSEGV, Segmentation fault.
0x2205303e in KIO::Connection::sendnow () from /usr/local/lib/libkio.so.6
(gdb) where
#0 0x2205303e in KIO::Connection::sendnow () from 
/usr/local/lib/libkio.so.6
#1 0x22052cf4 in KIO::Connection::send () from /usr/local/lib/libkio.so.6
#2 0x22048f86 in KIO::Slave::send () from /usr/local/lib/libkio.so.6
#3 0x220551fe in KIO::SimpleJob::start () from /usr/local/lib/libkio.so.6
#4 0x2205818a in KIO::TransferJob::start () from /usr/local/lib/libkio.so.6
#5 0x2205036a in KIO::Scheduler::startJobScheduled () from 
/usr/local/lib/libkio.so.6
#6 0x2204f83f in KIO::Scheduler::startStep () from 
/usr/local/lib/libkio.so.6
#7 0x2205287c in KIO::Scheduler::qt_invoke () from 
/usr/local/lib/libkio.so.6
#8 0x2125b0b4 in QObject::activate_signal () from 
/usr/X11R6/lib/libqt-mt.so.3
#9 0x2125afb1 in QObject::activate_signal () from 
/usr/X11R6/lib/libqt-mt.so.3
#10 0x214f4ee0 in QTimer::timeout () from /usr/X11R6/lib/libqt-mt.so.3
#11 0x212752db in QTimer::event () from /usr/X11R6/lib/libqt-mt.so.3
#12 0x212163eb in QApplication::internalNotify () from 
/usr/X11R6/lib/libqt-mt.so.3
#13 0x21216028 in QApplication::notify () from /usr/X11R6/lib/libqt-mt.so.3
#14 0x21c6218f in KApplication::notify () from 
/usr/local/lib/libkdecore.so.6
#15 0x212090e6 in QEventLoop::activateTimers () from 
/usr/X11R6/lib/libqt-mt.so.3
#16 0x211d190c in QEventLoop::processEvents () from 
/usr/X11R6/lib/libqt-mt.so.3
#17 0x212241be in QEventLoop::enterLoop () from 
/usr/X11R6/lib/libqt-mt.so.3
#18 0x21224116 in QEventLoop::exec () from /usr/X11R6/lib/libqt-mt.so.3
#19 0x2121656d in QApplication::exec () from /usr/X11R6/lib/libqt-mt.so.3
#20 0x100888d in main () at kjscript.cpp:504


####################################################################################################








Thanx,
-- BSP


Adriaan de Groot wrote:

>On Wed, 14 Jul 2004, Waldo Bastian wrote:
>  
>
>>On Wed July 14 2004 17:33, Yarram Sunil Kumar wrote:
>>    
>>
>>>     I have changed my code and running the QApplication::exec()  in
>>>main thread. There are other worker threads that does fetching (calls
>>>openURL).
>>>Each thread reads a list of urls and fetches them sequentially.
>>>      
>>>
>>DCOP (or KDE for that matter) does not support multiple threads.
>>    
>>
>
>That's an awfully broad statement to make. And untrue, since you can see
>KNode using threads quite successfully for several years now (KPilot does,
>too). The KDE libraries are not designed to be reentrant, though, so you
>have to be _very_ careful what you do with the libraries from other
>threads; basically, if it crashes, then you did something you shouldn't
>have. Use threads for computation - call KDE libraries only from the main
>thread.
>
>
> 
>  
>
>>>Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
>>>      
>>>
>
>
>
>  
>

 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic