none
AppDomain recovery after unhandled exception RRS feed

  • Question

  • is it possible to clone appdomain.current every minute into a backup var
    in case of unhandled exception

    and in appdomain.current.unhandledexception event continue execution from the last backup of the appdomain before the exception

    in order to recover into the state of the application before the exception,

    so that everything will be restore?

     

    any suggestions on how to do something like this?

    thanks

    Thursday, March 25, 2010 5:55 PM

Answers

  • > is it possible to clone appdomain.current

    There is no built-in function to do this.  (This would actually be quite beyond the capabilities of the .NET CLR and the Windows OS!)

    You would need to keep track of what needs to be saved and periodically write it to persistent storage.  Then if your program crashes, you could go back to the last save.  (Similar to what Office does to restore your work after a crash.)

     

     

    Friday, March 26, 2010 1:27 AM

All replies

  • > is it possible to clone appdomain.current

    There is no built-in function to do this.  (This would actually be quite beyond the capabilities of the .NET CLR and the Windows OS!)

    You would need to keep track of what needs to be saved and periodically write it to persistent storage.  Then if your program crashes, you could go back to the last save.  (Similar to what Office does to restore your work after a crash.)

     

     

    Friday, March 26, 2010 1:27 AM
  • i mean more to the direction of

    saving the thread stacks, heaps,locals, instruction pointer and all the memory of the application, every 2 minuted into a file,

    like some kind of crush dump, and when there is an unhandled exception , it will unload the crushed appdomain , load the file into memory and continue execution from it,

     

    maybe there are  some apis that can help in these classes,

    AppDomain

    ExecutionContext

    Process

    Thread

    ThreadHelper

    HostExecutionContextManager

    Debugger

    StackFrame

    InstanceData

    ProcessThread

    ApplicationActivator

    Application

     

    maybe dbghelp.dll & kernel32.dll have stuff that can help

     

    maybe the new .net 4 clr hosting api


    also ProcDump from sysinternals has a feature (-r) that reflect the process, maybe i'm looking for something like this,

    like spawn a reflection of my process, resuming the reflected process, and exit the crushed process (needs win7)

     

    i wonder how procdump does it

     

    any suggestions on how to implement something like that?

     

    thanks

    Friday, March 26, 2010 1:29 PM
  • Why would you want something like this? I can imagine this is required in-case you are hosting assemblies. If you are not hosting then I would fix the real exception.

     

    If you are hosting then you use the ICLRPolicyManager has policy for un-handled exceptions

     

    Thanks

    naveen

    http://naveensrinivasan.com

    Friday, March 26, 2010 7:12 PM
  • i dont think that ICLRPolicyManager is what i need,

    i want to do exactly what procdump does when you pass it -r as argument

    which makes him clone the process (a new feature in windows 7),

    and then i want to resume it

     

    i looked into procdump import table

    there must be api there that is new in windows 7, or existing function that has new parameters

    probably one that is in it's import table:

     

    dbghelp.dll

    MiniDumpWriteDump

     

    KERNEL32.dll

    WaitForSingleObject

    SetEvent

    OpenProcess

    SizeofResource

    FormatMessageW

    GetExitCodeProcess

    GetTimeFormatW

    GetFileAttributesW

    TerminateProcess

    GetModuleFileNameW

    CreateFileW

    GetLastError

    Process32FirstW

    CreateEventW

    GetSystemInfo

    WaitForMultipleObjects

    Process32NextW

    ContinueDebugEvent

    GetCurrentProcess

    WaitForDebugEvent

    CloseHandle

    DeleteFileW

    GetSystemTime

    ExpandEnvironmentStringsW

    CreateFileA

    ReadFile

    GetProcessHeap

    SetEndOfFile

    HeapSize

    FlushFileBuffers

    SetStdHandle

    WriteConsoleW

    GetConsoleOutputCP

    WriteConsoleA

    LoadLibraryA

    SystemTimeToTzSpecificLocalTime

    CreateProcessW

    LoadResource

    FindResourceW

    DebugActiveProcessStop

    DebugActiveProcess

    GetDateFormatW

    LocalFree

    GetFullPathNameW

    LocalAlloc

    GetProcAddress

    LoadLibraryW

    GetModuleHandleW

    GetCommandLineW

    CreateToolhelp32Snapshot

    ResumeThread

    HeapFree

    HeapAlloc

    ExitThread

    CreateThread

    UnhandledExceptionFilter

    SetUnhandledExceptionFilter

    IsDebuggerPresent

    GetCPInfo

    InterlockedIncrement

    InterlockedDecrement

    GetACP

    GetOEMCP

    IsValidCodePage

    TlsGetValue

    TlsAlloc

    TlsSetValue

    TlsFree

    SetLastError

    GetCurrentThreadId

    HeapCreate

    VirtualFree

    DeleteCriticalSection

    LeaveCriticalSection

    EnterCriticalSection

    VirtualAlloc

    HeapReAlloc

    Sleep

    ExitProcess

    WriteFile

    GetStdHandle

    GetModuleFileNameA

    RtlUnwind

    LCMapStringW

    WideCharToMultiByte

    GetConsoleCP

    GetConsoleMode

    RaiseException

    FreeEnvironmentStringsW

    GetEnvironmentStringsW

    SetHandleCount

    GetFileType

    GetStartupInfoA

    QueryPerformanceCounter

    GetTickCount

    GetCurrentProcessId

    GetSystemTimeAsFileTime

    LCMapStringA

    MultiByteToWideChar

    GetStringTypeA

    GetStringTypeW

    GetLocaleInfoA

    InitializeCriticalSectionAndSpinCount

    SetFilePointer

     

     

    USER32.dll

    IsHungAppWindow

    EnumWindows

    IsWindowVisible

    GetWindowThreadProcessId

    SetCursor

    DialogBoxIndirectParamW

    LoadCursorW

    InflateRect

    GetDlgItem

    EndDialog

    GetSysColorBrush

    SendMessageW

    SetWindowTextW

     

     

    GDI32.dll

    StartPage

    GetDeviceCaps

    SetMapMode

    StartDocW

    EndDoc

    EndPage

     

     

    COMDLG32.dll

    PrintDlgW

     

     

    ADVAPI32.dll

    LookupPrivilegeValueW

    OpenProcessToken

    RegSetValueExW

    RegCloseKey

    RegQueryValueExW

    RegCreateKeyW

    AdjustTokenPrivileges

     

     

    Sunday, March 28, 2010 11:45 AM
  • There is some discussion of process reflection in this video: http://channel9.msdn.com/shows/Going+Deep/Mark-Russinovich-Inside-Windows-7-Redux/

    Mark Russinovich briefly says something about the reflected process not being runnable.  So I don't think this will help you.  (Not surprising... even if you put all the process's memory back, there is still kernel and hardware state that cannot be saved and/or restored.  You can't "unlaunch the missiles" as they say in transaction processing.)

    That being said, out of curiosity I attempted to find information about a process reflection API.  I could find any and therefore I wouldn't be surprised if it is undocumented.  If anyone else can find it, please post the link!

    > probably one that is in it's import table

    Since procdump also works on systems other than Windows 7, it might very well be a new API that is not in the import table but instead is loaded dynamically at runtime.

     

    Tuesday, March 30, 2010 1:33 AM
  • i used strings to find the dynamic load function:

    ntdll.dll     (win 7 or later)
    RtlCreateProcessReflection

     

    i found it's signatue with IDA and it's PDB

    int __cdecl RtlCreateProcessReflection(int, int, int, int, int, int, int, int, __int64, void *)

    which itself uses memcpy and

    NtDuplicateObject

    which calls to ObDuplicateObject inside ntoskrnl.exe

     

    but i don't really understand assembler that good

     

    maybe it's possible to p/invoke it

    like this (with the p/invoke interop assistant):

    [DllImport("ntdll.dll", EntryPoint="RtlCreateProcessReflection", CallingConvention=CallingConvention.Cdecl)]
    public static extern int RtlCreateProcessReflection(int param0, int param1, int param2, int param3, int param4, int param5, int param6, int param7, long param8, IntPtr param9);
    

    and you're right it's undocumented

    so i don't know what the parameters mean,

    and still it's probably not runnable

    Monday, April 5, 2010 9:54 AM
  • something i found on process reflection:

    Process Reflection

    Process reflection is a mechanism to help dumping information about processes that are likely to hang or that are long running ones. Prior to Windows 7, that process and all of its threads had to be suspended until the dump was completed. Process reflection now clones a process before dumping it. The cloning is based on fork(), which basically copies a process, but all its pages are marked for copy-on-write, i.e. whenever the original process updates one of its memory data, the memory manager will copy the page so that the original process see the latest changes while the clone does not. On the other hand, this mechanism allows the dump proces to read the state of the cloned process without any disturbance.

    Monday, April 5, 2010 2:54 PM
  • does anyone have more ideas, of what can be done?

     

    thanks

    Thursday, April 8, 2010 8:27 PM
  • Hello

    As BinaryCoder says in the first reply, this cannot be accomplished in the unhandled exception handler.

    You can restore data of the application, but if you want to restore its execution, you should do it in the first chance exception handler, not in the unhandled exception (2nd chance exception) handler.


    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 20, 2010 9:43 AM
    Moderator