Page 1 of 1

CEF crash on logout

PostPosted: Mon Aug 31, 2020 10:36 am
by amukher1
hi,
I am receiving the attached crash dump on exiting the application. This looks to be a crash happening in libcef.dll in windows 7 and win10.
Can you please let me know what is the best way to force terminate a CEF based C++ windows application.
Currently this is what i have in my exit code

void killProcessByName(wstring filename)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof(pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
if (wcscmp(pEntry.szExeFile, filename.c_str()) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
(DWORD)pEntry.th32ProcessID);
if (hProcess != NULL)
{
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}

void ApplicationLogOutCommand::execute(const jsoncons::wjson &input, jsoncons::wjson &out) {

try {
CefShutdown();
killProcessByName(L"sample-cef.exe");
}
catch (std::runtime_error) {
exit(0);
}
}

Re: CEF crash on logout

PostPosted: Mon Aug 31, 2020 11:01 am
by magreenblatt
amukher1 wrote:Can you please let me know what is the best way to force terminate a CEF based C++ windows application.

Why do you want to do this, instead of shutting down normally?

Re: CEF crash on logout

PostPosted: Mon Aug 31, 2020 11:14 am
by amukher1
Thanks . Yes so i need steps to shutdown gracefully and clear the memory occupied by CEF process. So that a dump of the memory after logout should not reveal any data. Since for a C++ based windows application the only way to fully clear memory is to terminate the process. When i do that i face this crash with libcef.
If i use cefshutdown() and give a timer of 1 second before calling terminate process i dont see the crash. Is there any other way to avoid the crash with libcef and can you let me know why it crashes in libcef?

Re: CEF crash on logout

PostPosted: Mon Aug 31, 2020 11:45 am
by magreenblatt
How are you running the CEF message loop? Where are you calling CefShutdown from?

Re: CEF crash on logout

PostPosted: Tue Sep 22, 2020 1:40 pm
by paulbanks
I would like to know that too. I think it's possible to fix your problem easily.

Re: CEF crash on logout

PostPosted: Tue Sep 22, 2020 2:29 pm
by ndesktop
Well, first of all, you might want to exclude the caller pid with some GetCurrentProcessId() != pEntry.th32ProcessID, because you are just killing your own process.

Assuming CefShutdown is called correctly but still in some cases processes remains orphan, are hanged, break in debuggers etc. and you need a cleanup routine, continue reading.

I have some similar code which does this. Assuming we are in the browser process (a simple check on --type argv will do):
- use TH snapshot enumeration and get all the matching processes in a std::map
- exclude current process if this is executing from a browser
- exclude processes which are *not* current process and their th32ParentProcessID != GetCurrentProcessId() to avoid killing subprocesses from another instance of the executable
- now for fine tuning, and for each process use winternl.h and
OpenProcess with PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ;
NtQueryInformationProcess with ProcessBasicInformation to get PebBaseAddress;
then ReadProcessMemory on PebBaseAddress to get ProcessParameters (RTL_USER_PROCESS_PARAMETERS);
then ReadProcessMemory on ProcessParameters to get RTL_USER_PROCESS_PARAMETERS::CommandLine;
then ReadProcessMemory on RTL_USER_PROCESS_PARAMETERS::CommandLine::ProcessParameters to get the command line
then finally CommandLineToArgvW to extract the argc/argv-like arguments, and lookup for --type argument to know what kind of subprocess (or browser).

I need this because actually my browser process is spawning another process (let's call it watchdog) which receives the browser PID and opens the browser process and WFSO on it.
If signaled with something else than WAIT_OBJECT_0, then kicks in the logic above (collect processes - gather only browser & its subprocesses - read command lines - Muttley gets a medal).

The code is not trivial, but there are a number of good lead samples which you can consult to achieve your goal.

Re: CEF crash on logout

PostPosted: Tue Sep 29, 2020 4:17 pm
by rdh
When we shutdown, I have code that waits until each outstanding browser is closed and the client handler is destroyed. However, try as I might, I have found that I could sometimes wait until the cows come home for all that to occur. I finally just decided to wait for a max of 5 seconds. If after 5 seconds I find I still have outstanding browsers, I simply skip the call to CefShutdown.

So far, I have noticed nothing wrong with not calling CefShutdown. All my subprocesses are cleaned up, which was my biggest worry. Still, to make sure they don't hang around, I have them self terminate if they find my app process is no longer valid.

I don't think skipping the call is so bad. After all, I run in a Visual Studio debugger all the time and I rarely exit the app gracefully - I just stop debugging and the debugger kills the process. End result for CEF is CefShutdown isn't called then either.

Re: CEF crash on logout

PostPosted: Tue Sep 29, 2020 4:56 pm
by magreenblatt
Not calling CefShutdown can result in cache corruption. If you’re not writing any files to disk then it should be fine.

Re: CEF crash on logout

PostPosted: Wed Sep 30, 2020 12:28 am
by amukher1
Thanks a lot . Yes a sleep() command along with cefshutdown solved my issue. Now the app can exit gracefully.

Re: CEF crash on logout

PostPosted: Wed Oct 14, 2020 6:05 am
by miro
amukher1 wrote:Thanks a lot . Yes a sleep() command along with cefshutdown solved my issue. Now the app can exit gracefully.

Hi,
i have the same issue, where do you put sleep() in your code, before immediately before calling CefShutdown(), is that right?

best regards
miro