DinkToPdf icon indicating copy to clipboard operation
DinkToPdf copied to clipboard

StopThread() cannot stop the newly opened background thread, resulting in unlimited thread growth and eventually breaking the Linux system limit.

Open hkszlq opened this issue 2 weeks ago • 0 comments

in src/DinkToPdf/SynchronizedConverter.cs

private void StopThread() { lock (startLock) { if (conversionThread != null) { kill = true; while (conversionThread.ThreadState == ThreadState.Stopped) { } conversionThread = null; } } }

 private void StartThread()
    {
        lock (startLock)
        {
            if (conversionThread == null)
            {
                conversionThread = new Thread(**Run**)
                {
                    IsBackground = true,
                    Name = "wkhtmltopdf worker thread"
                };

                kill = false;

                conversionThread.Start();
            }
        }
    }

private void Run() { while (!kill) { //get next conversion taks from blocking collection Task task = conversions.Take();

            lock (task)
            {
                //run taks on thread that called RunSynchronously method
                task.RunSynchronously();

                //notify caller thread that task is completed
                Monitor.Pulse(task);
            }
        }
    }

hkszlq avatar Jun 22 '24 00:06 hkszlq