EAK TEAM
EAK TEAM

Reputation: 7482

App Crash instantly without exception or log (seems like xamarin/mono bug)

It's a weird situation here :) , I have implemented this code in my fragment to scan TCP Ports in multithread environment , it crash when debugging , and sometimes crash in release mode too with thoose messages :

E/art (12972): Nested signal detected - original signal being reported

F/art (12972): art/runtime/fault_handler.cc:117] Check failed: !initialized_

tried to set Target Android to 23 that didn’t work.

tried running adb shell setprop debug.mono.env MONO_DEBUG=soft-breakpoints that didn’t work.

tried to compile using different Android API's Versions

ANY IDEA WHY IS THIS HAPPENING ??

HERE IS MY CODE:

    public void start()
    {
        for (int i = 0; i < 50; i++)
        {
            Task.Run(() => RunScanTcp());
            //ThreadPool.QueueUserWorkItem(RunScanTcp);
            //Thread thread = new Thread(new ThreadStart(RunScanTcp));
            //thread.Start();
        }
    }


    public void RunScanTcp()
    {
        while (abort != true)
        {
                port = port + 1;
                Log.Info("PORT SCANNER", port.ToString());
        }
    }

    public class PortList
    {
        private int start;
        private int stop;
        private int ports;
        private static readonly object _syncRoot = new object();

        public PortList(int starts, int stops)
        {
            start = starts;
            stop = stops;
            ports = start;
        }

        public bool MorePorts()
        {
            lock (_syncRoot)
            {
                return (stop - ports) >= 0;
            }
        }

        public int NextPort()
        {
            lock (_syncRoot)
            {
                if (MorePorts())
                {
                    return ports++;
                }
                return -1;
            }
        }
    }

Im compiling using :

Android Version (Android 7.1 Nougat)

Minimum Android Version :

Android 4.1 (API Level 16 - Jelly Bean)

Target Android Version:

Compile Using SDK Version

UPDATE :

After Visual Studio Update 15.2 (26430.12) , and Xamarin 4.5.0.476 - 30/05/2017(dd/mm/yyyy) the application crashed while is connected to debugger too...

Here is the debugger output :

referenceTable GDEF length=814 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=47302 1
referenceTable head length=54 1
referenceTable GDEF length=428 1
referenceTable GSUB length=2302 1
referenceTable GPOS length=43252 1
referenceTable head length=54 1
referenceTable GDEF length=808 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=49128 1
referenceTable head length=54 1
referenceTable GDEF length=808 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=47320 1
referenceTable head length=54 1
05-31 04:31:51.590 F/art     (17427): art/runtime/fault_handler.cc:117] Check failed: !initialized_ 

Thank's All of you ...

Upvotes: 10

Views: 6026

Answers (4)

EAK TEAM
EAK TEAM

Reputation: 7482

The problem seems to be in xamarin.android platform because after last update 6/9/2017 Visual Studio 2017 version 15.2 (26430.13) it dont crash anymore ... anyway thanks for your time .. This bug happens when tried to create (n) number of threads/task ...

Upvotes: 0

woelliJ
woelliJ

Reputation: 1504

As you described it is working without that part of your code ... so the error must be somewhere in your code.

let's look at it.

public void start()
{
    for (int i = 0; i < 50; i++)
    {
        Task.Run(() => RunScanTcp());
    }
}


public void RunScanTcp()
{
    while (abort != true)
    {
            port = port + 1;
            Log.Info("PORT SCANNER", port.ToString());
    }
}

In the start method you start 50 tasks - and not awaiting any of them - which means, that possibly all tasks run in parallel (at least definitely some tasks try to access port at the same time).

What those tasks execute is a method that accesses port which must be a field of the containing class.

Accessing a shared field from multiple tasks / threads is never a good idea without a lock.

Try using the following code instead (if you really need the parallel tasks for some reason).

public class YourClass
{

    private bool abort;
    private int port;
    private object portLock = new object();

    public void start()
    {
        for (int i = 0; i < 50; i++)
        {
            Task.Run(() => RunScanTcp());
        }
    }


    public void RunScanTcp()
    {
        while (abort != true && this.port < int.MaxValue)
        {
            lock (this.portLock) {
                port = port + 1;
                Log.Info("PORT SCANNER", port.ToString());
            }
        }
    }

}

Possibly a better approach is to only use one task and somewhere await it.

public class YourClass
{

    private bool abort;
    private int port;
    private object portLock = new object();

    public Task start()
    {
        return Task.Run(() => RunScanTcp());
    }


    public void RunScanTcp()
    {
        lock (this.portLock) {
            while (abort != true && this.port < int.MaxValue)
            {
                port = port + 1;
                Log.Info("PORT SCANNER", port.ToString());
            }
        }
    }

}

If that solves your current problem you might want to add another question about how to properly solve what you're actually trying to solve (since your current code basically just increments the port until it is aborted or the app crashes (since it counted above the Int32 MaxValue)

Upvotes: 3

user3130628
user3130628

Reputation: 517

So I came across an issue just like that with Xamarin and VS. it turns out that not all error are logged into the VS debugging console, but they are in the device log. What I had to do that time was to go and use the emulator on my machine and run it while having the device log ON. and stopped the logging right after the crash. In that log it was clear what the issue was (which in my case was a misconfigured permission). Hope that helps.

Upvotes: 2

Related Questions