Embedded server application stopped working after exiting

I have an application that stores some data in a firebird database. I use the built-in Firebird server and EntityFramework, and everything works fine, but when I close the application using the x button on the form, I get a Windows system message, the application stopped working, and I can not catch this exception. I have an UnhandledExceptionHandler application in my application:

// Add handler for UI thread exceptions Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException); // Force all WinForms errors to go through handler Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //This handler is for catching non-UI thread exceptions AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); .....some other code.......... Application.Run(new MainForm()); 

But such an exception never came across them. So I went to the Windows event log and found this xml view of the error event there:

 - <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> - <System> <Provider Name="Application Error" /> <EventID Qualifiers="0">1000</EventID> <Level>2</Level> <Task>100</Task> <Keywords>0x80000000000000</Keywords> <TimeCreated SystemTime="2017-03-14T23:06:25.000000000Z" /> <EventRecordID>36077</EventRecordID> <Channel>Application</Channel> <Computer>MYPC</Computer> <Security /> </System> - <EventData> <Data>MyApp.exe</Data> <Data>1.0.0.0</Data> <Data>58c7a3f0</Data> <Data>fbintl.DLL</Data> <Data>2.5.5.26952</Data> <Data>5644432f</Data> <Data>c0000005</Data> <Data>00004e9c</Data> <Data>1d64</Data> <Data>01d29d1797fb7f0d</Data> <Data>G:\Programming\WorkSpace\C#\MyApp\bin\x86\Debug\MyApp.exe</Data> <Data>G:\Programming\WorkSpace\C#\MyApp\bin\x86\Debug\FireBirdEmbeddedServer\intl\fbintl.DLL</Data> <Data>d84a6ca6-090a-11e7-8151-005056c00008</Data> </EventData> </Event> 

As you can see, something went wrong with fbintl.DLL when the application was already closed. So, how can I get a more detailed description of this problem?

UPD I make the application shorter to find the cause of my problem - now ONLY this EF code runs before the application closes

  public async Task GetAutoAnswerTemplate() { try { using (var db = new FirebirdDbContext(embeddedConnectionString)){ //Async or sync methods doesn't affect to my problem AutoAnswerTemplate template = await dbContext.AutoAnswerTemplate.FirstOrDefaultAsync(); return template?.AutoAnswer_body; } } catch (Exception ex) { throw new EmbeddedFbDataBaseTools.EmbeddedDbException( "Error while getting auto answer template" + "\r\n" + ex.Message, ex); } } 

Where is the FirebirdDbContext:

 public class FirebirdDbContext : DbContext { public FirebirdDbContext(string connString) : base(new FbConnection(connString), true) { //* The Entity initializer is bugged with Firebird embedded: http://stackoverflow.com/q/20959450/2504010 so I didn't use default---> // Database.SetInitializer<FirebirdDBContext>(new CreateDatabaseIfNotExists<FirebirdDBContext>()); Database.SetInitializer<FirebirdDbContext>(new MyCreateDatabaseIfNotExists()); } public DbSet<AutoAnswerTemplate> AutoAnswerTemplate { get; set; } public DbSet<User> User { get; set; } } class MyCreateDatabaseIfNotExists : IDatabaseInitializer<FirebirdDbContext> { public void InitializeDatabase(FirebirdDbContext context) { if (!context.Database.Exists()) { context.Database.Create(); } } } 

And communication parameters

  public static string GetEmbeddeddefaultConnectionString() { FbConnectionStringBuilder builder = new FbConnectionStringBuilder { ServerType = FbServerType.Embedded, DataSource = "localhost", Port = 3050, Database = EmbeddedDbPath, //Path to embedded db ClientLibrary = EmbeddedServerDllPath, UserID = "SYSDBA", Password = "masterkey", Charset = "WIN1251", Dialect = 3, ConnectionLifeTime = 15, Pooling = true, MinPoolSize = 0, MaxPoolSize = 50 }; return builder.ToString(); } 

NEW UPDATE 04/25/2017

I made a simple application with built-in db Firebird that shows an error. U can be found here

The application creates the built-in firebird database and connects to it in the background thread (Task TPL), and after shutting down (_bgTask.Status == TaskStatus.RanToCompletion) close the application and get an error message.

+5
source share
3 answers

In your connection string, you specified a character set and enabled the connection pool:

 FbConnectionStringBuilder builder = new FbConnectionStringBuilder { … Charset = "WIN1251", … Pooling = true, … }; 

The combination of these two settings causes an error; not in your own code, but in FirebirdSQL. I still found three ways to solve this problem. You can do it:

  • Call the static FbConnection.ClearAllPools() method right before your application terminates (and leave the connection pool turned on):

     private static void AppExit(object sender, EventArgs e) { … FbConnection.ClearAllPools(); } 
  • Disable connection pooling by setting Pooling = false .

  • Since the error is triggered in fbintl.dll , which seems to be dealing with character sets / internationalization, you can simply omit the connection string parameter Charset (although I don't know what the consequences may be).

The last two sentences are workarounds. I would probably go with parameter No. 1, since it seems clean, it allows you to maintain the connection pool (which is usually good) and specify the required encoding.

Please note that you can only see the exception if you run the application with the debugger attached. In production, an exception might remain silent and completely invisible.

+3
source

Your exception handling only works in 2 cases, exceptions and memory exceptions. In this case, the behavior should correspond to the behavior you described (the system message application has stopped working).
On exceptions, EF happens quite often when you serialize entities (json, xml, ...) with lazy loading enabled. Do you serialize objects during exit?

If this is not your case, you can check out the other two things:

  • Do you ever shoot two event handlers?
  • Are you throwing exceptions in the exception handler?
+1
source

It was a bit difficult to debug your code _prepareAppTask = new TaskFactory().StartNew occasionally raises a race condition, which in turn raises an exception, which I described in my initial post.

It’s also not obvious that you throw and break and exclude backstage when I tried to run the DB code in the main thread, which caused unexpected behavior and made me solve the problem.

In addition, I do not see critical problems in your code , which can lead to an error. It seems that either firebird itself or the EF provider is causing a problem when the application terminates. This has nothing to do with synchronization or asynchronous code - nothing like that. I was able to play it just by running a console application.

The application crashes when exiting with the Faulting module name: fbintl.DLL only if a connection to the database was created and the code was accessed by the database. The answer to the Fault-tolerant module, what does it mean and why is this happening? makes me think the problem is with either firebase or the EF provider.

Have you tried other ways to connect to firebase?

And if you only need to get rid of this unpleasant exception, then this is the hack that worked for me:

 private static void AppExit(object sender, EventArgs e) { ... // To ensure pending DB operations are processed. Not sure that needed. int timeout = 1000; Thread.Sleep(timeout); Process.GetCurrentProcess().Kill(); } 

Killing a process right before it exits prevents an error from occurring.

+1
source

Source: https://habr.com/ru/post/1265460/


All Articles