Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to host Plug-ins safely with .NET 2.0

Tags:

c#

.net

hosting

I am writing a chess game which allows two programs compete, the player needs to write a DLL and expose a function to tell the main application where his player will move next, suppose the function looks like this

public static void MoveNext(out int x, out int y, out int discKind);

The player's DLL can be written using C# or C++.

In the chess game application, I start a new thread to call the function that the player's DLL exposed to get where he will move in a turn, and I start a timer to prevent the player timeouts, if a player timesout i will kill the corresponding thread by following APIs

thread.Abort();
thread.Join();

I have the following issues as described below:

  1. The thread cannot be killed with 100% assurance (it depends on the player's code)

  2. During test I found that, if the player uses a deep recursions (and if there is memory leak in the player's program), the memory usage of the host application will increase and then the host application will be terminated without any exceptions.

Are there any techniques, ideas or methods that can handle the above issues?

From this CodeInChaos suggested to load player's DLL into separate domain and then unload it when necessary, I am not sure if it still works for the unmanaged DLL (C++) and if it will cause a low efficiency?

like image 506
Carlos Liu Avatar asked Mar 25 '11 01:03

Carlos Liu


People also ask

Is .NET Framework obsolete?

Microsoft's support policy has suggested that the framework will not be developed and updated any longer; however, existing applications will still be supported as long as they are installed in a supported version of Windows.

Is .NET 4.8 the last?

NET Framework 4.8 was the final version of . NET Framework, future work going into the rewritten and cross-platform .

What is latest .NET Framework version?

NET Framework 4.8. To determine the installed . NET version, use the following Release DWORD: 528449 (Windows 11 and Windows Server 2022)

What does NET Framework 4.8 do?

NET Framework 4.8, the latest version of the company's application development framework for Windows. The update brings a number of bug fixes, security patches, and improvements to the Common Language Runtime, ASP.NET, Windows Forms, Windows Presentation Foundation, and Windows Communication Foundation.


2 Answers

An unhandled exception in their AppDomain will still cause your program to terminate in .Net 2.0. You get a chance to respond to the exception through an event handler but not the ability to handle it.

Your best bet is to use processes for the kind of isolation you're looking for.

like image 178
SuperIronBob Avatar answered Nov 14 '22 22:11

SuperIronBob


If you can ensure your plugin DLL's are always managed code, then you have the option of createing a new application domain in your main application logic and loading the assembly containing the plugin into that domain.

This then gives you the option of trapping unhandled excpetions in that specific app domain and you then have the option of Unloading that whole app domain. That way you can cope with other peoples application plugins misbehaving and throwing exceptions. you also gain the option of specifying partial trust to further restrict what a plugin can do.

However this will not help if you cannot enforce the use of managed code plugins, and the earlier option of a set of seperate processes would be more apropriate.

Reading your post agin it seems you have some quality issues with the plugins you have to use. If you must cope with such buggy plugins I would take the previous advice and go with seperate processes.

like image 24
Pete Stensønes Avatar answered Nov 14 '22 22:11

Pete Stensønes