3

Something really strange is happening. If i place a break point after a certain line of code, when debugging, the debug session stops as if i hit the "Stop" button.
What's really strange is that, if i skip that function altogether, jumping over it, the code does not crash...

I'm using .Net Core 2, running in a fully updated VS For Mac on any VS (windows and Mac), using C# 7.1

heres the code:

        var connectionToUse = new SqlConnection(string.Format(str, dbName));
        try
        {
            SqlCommand command = new SqlCommand();
            command.Connection = connectionToUse;
            command.CommandText = @"SELECT * from myTable";
            await connectionToUse.OpenAsync(); //CANT GET PAST THIS LINE HERE
            var r1 = await command.ExecuteReaderAsync();
            while (await r1.ReadAsync())
            {
                //MORE CODE
            }
            r1.Close();
            await Task.Delay(15000);
        }
        catch (Exception ex)
        {
            //NEVER ENTERS HERE
        }
        finally
        {
            if (connectionToUse.State != ConnectionState.Closed)
            {
                connectionToUse.Close();
            }
        }

Edit1:
Using .Open(), no async, works perfectly. But the problem moves to the r1.ReadAsync() line...

Edit2:
prior to that code, the following one runs perfectly

    private async Task<Dictionary<int, string>> MapDatabases()
    {
        Dictionary<int, string> databases = new Dictionary<int, string>();

        SqlConnection mainConn = new SqlConnection(string.Format(str, "Master"));
        SqlCommand command = new SqlCommand();
        command.CommandText = "SELECT name,database_id FROM sys.databases WHERE database_id = 5";
        command.Connection = mainConn;
        try
        {
            await mainConn.OpenAsync();
            SqlDataReader reader = await command.ExecuteReaderAsync();
            while (await reader.ReadAsync())
            {
                databases.Add(reader.GetInt32(1), reader.GetString(0));
            }
            reader.Close();
            mainConn.Close();
        }
        finally
        {
            if (mainConn.State != ConnectionState.Closed)
            {
                mainConn.Close();
            }
        }
        return databases;
    }

EDIT3:
Also reproducible on VS 2017 fully updated.

EDIT4:
Apparently theres something wrong with the await statement. I found out that the code will crash in the first subsequent await, no matter where it is

Leonardo
  • 10,737
  • 10
  • 62
  • 155
  • Does the database exist? When you debug the code, is it using the same connection string (from the relevant appsettings.json file) as when you run it without debugging? – Jamie Taylor Feb 06 '18 at 14:06
  • @JamieTaylor Yes, the database exists and the code works as expected if i don't use the async versions – Leonardo Feb 06 '18 at 14:08
  • Maybe a debugger bug. I would report it on the [Visual Studio for Mac developer forum](https://developercommunity.visualstudio.com/spaces/41/index.html) and attach the IDE log and the debugger output. Also a small repro project, if possible, would be useful too. – Matt Ward Feb 06 '18 at 14:37
  • @MattWard damn, this happens on VS 2017 for windows also... – Leonardo Feb 06 '18 at 15:09

1 Answers1

1

I kinda figured out... I was spanning new threads using Task.StartNew. After changing to Task.Run things started working properly. Can't figure out why the debug session was crashing though... Since the code is working as expected, i'm accepting this as answer.

Leonardo
  • 10,737
  • 10
  • 62
  • 155
  • `StartNew` uses the `Current` synchronization context, so your work (probably) eventually get run on UI thread, more on this here: https://blog.stephencleary.com/2013/08/startnew-is-dangerous.html – VMAtm Feb 12 '18 at 22:27