fsprojects / Rezoom.SQL

Statically typechecks a common SQL dialect and translates it to various RDBMS backends
MIT License
669 stars 25 forks source link

Object reference not set to an instance of an object error while using the TP in Mono #20

Open tamizhvendan opened 7 years ago

tamizhvendan commented 7 years ago

Hi @rspeele,

Thanks a lot for the awesome work.

Today, When I tried to use Rezoom type provider in my Mac using Mono (both 5.0.1 & 5.2.0) I am getting an "Object reference not set to an instance" error

To reproduce it, open the TypeProviderUsers.sln file in the Rezoom.SQL in either visual studio code or VS for Mac

/~/temp/Rezoom.SQL/src/TypeProviderUsers/TypeProviderUser.SQLite/Shared.fs(18,18): Error FS3033: The type provider 'Rezoom.SQL.Provider.Provider' reported an error: Object reference not set to an instance of an object (FS3033) (TypeProviderUser.SQLite)

SQLLite Type Provider image

Postgres Type Provider image

tamizhvendan commented 7 years ago

I guess it is something similar to this issue. Some internal code while initializing the type provider is causing the issue. I tried to attach debugger and troubleshoot but it didn't work out as we can't open more than one instance of VS for Mac :(

Is there any way that I can troubleshoot this to find the actual error?

rspeele commented 7 years ago

Hi,

Thanks for the bug report. A good way to troubleshoot it further would be to use the type provider from a .fsx script file and attach the debugger to fsi.exe. Rezoom.SQL.Provider has an example of this if you F5 it in debug configuration.

tamizhvendan commented 7 years ago

Thanks @rspeele

Unfortunately, it didn't work!

image


/~/temp/Rezoom.SQL/src/Rezoom.SQL.Provider/user.fsx(15,10): error FS3033: The type provider 'Rezoom.SQL.Provider.Provider' reported an error: Object reference not set to an instance of an object

mono_os_mutex_lock: pthread_mutex_lock failed with "Invalid argument" (22)
Stacktrace:

  at <unknown> <0xffffffff>
  at System.IO.KqueueMonitor.CleanUp () [0x00053] in <70507a44a84041d599bbfa8f3d0ea5b1>:0
  at System.IO.KqueueMonitor.DoMonitor () [0x00051] in <70507a44a84041d599bbfa8f3d0ea5b1>:0
  at System.IO.KqueueMonitor.<Start>b__5_0 () [0x00000] in <70507a44a84041d599bbfa8f3d0ea5b1>:0
  at System.Threading.ThreadHelper.ThreadStart_Context (object) [0x00014] in <c9f8153c41de4f8cbafd0e32f9bf6b28>:0
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00071] in <c9f8153c41de4f8cbafd0e32f9bf6b28>:0
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00000] in <c9f8153c41de4f8cbafd0e32f9bf6b28>:0
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object) [0x0002b] in <c9f8153c41de4f8cbafd0e32f9bf6b28>:0
  at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <c9f8153c41de4f8cbafd0e32f9bf6b28>:0
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) [0x0004d] in <c9f8153c41de4f8cbafd0e32f9bf6b28>:0

Native stacktrace:

    0   mono                                0x000000010f9f6061 mono_handle_native_crash + 257
    1   libsystem_platform.dylib            0x00007fff8c5afb3a _sigtramp + 26
    2   ???                                 0x000070000cd9bfe0 0x0 + 123145517907936
    3   libsystem_c.dylib                   0x00007fff8c434420 abort + 129
    4   mono                                0x000000010fbcd591 mono_log_write_logfile + 353
    5   mono                                0x000000010fbe4740 monoeg_g_log + 208
    6   mono                                0x000000010fac63d1 mono_marshal_find_in_cache + 129
    7   mono                                0x000000010face23f mono_marshal_get_native_wrapper + 223
    8   mono                                0x000000010f9456f0 mono_jit_compile_method_inner + 384
    9   mono                                0x000000010f94884a mono_jit_compile_method_with_opt + 714
    10  mono                                0x000000010f9f866f common_call_trampoline + 1199
    11  mono                                0x000000010f9f8189 mono_magic_trampoline + 73
    12  ???                                 0x000000010fdad39e 0x0 + 4560966558
    13  ???                                 0x0000000118e3a5c9 0x0 + 4712539593
    14  ???                                 0x0000000118d8aa1e 0x0 + 4711819806
    15  ???                                 0x0000000118d8a7f5 0x0 + 4711819253
    16  mscorlib.dll.dylib                  0x00000001119cde11 System_Threading_ExecutionContext_Run_System_Threading_ExecutionContext_System_Threading_ContextCallback_object_bool + 33
    17  mono                                0x000000010f94c505 mono_jit_runtime_invoke + 2245
    18  mono                                0x000000010fb4dd48 do_runtime_invoke + 88
    19  mono                                0x000000010fb19a9e start_wrapper + 766
    20  libsystem_pthread.dylib             0x00007fff8c5b993b _pthread_body + 180
    21  libsystem_pthread.dylib             0x00007fff8c5b9887 _pthread_body + 0
    22  libsystem_pthread.dylib             0x00007fff8c5b908d thread_start + 13

Debug info from gdb:

(lldb) command source -s 0 '/tmp/mono-gdb-commands.TCzxrT'
Executing commands in '/tmp/mono-gdb-commands.TCzxrT'.
(lldb) process attach --pid 32058
error: attach failed: unable to attach

The application was terminated by a signal: SIGHUP
tamizhvendan commented 7 years ago

I tried to troubleshoot this using printfn as suggested.

For this code in user.fsx

type QueryWithNullablePar = SQL<"""
    select * from Users u
    where u.Name is @name
""", "user-migrations">

I put printfn in these places

Provider.fs

do
        printfn "initializing #2" // <----

        let tys = [ sqlTy; modelTy ]
        tmpAssembly.AddTypes(tys)
        this.AddNamespace(rootNamespace, tys)
        modelCache.Invalidated.Add(fun _ -> this.Invalidate())
        this.Disposing.Add(fun _ -> modelCache.Dispose())

        printfn "init success" // <----

    static do

        printfn "initializing #1" // <----

        System.AppDomain.CurrentDomain.add_AssemblyResolve(fun _ args ->
            AssemblyResolver.resolve args.Name |> Option.toObj)

TypeGeneration.fs

let generateSQLType (generate : GenerateType) (sql : string) =
  printfn "generating SQL Type" // <----
  // ...
  provided.AddMember <| generateCommandMethod generate commandEffect commandType commandCtorMethod

    printfn "generated SQL type" // <----

    provided

Here is the output that I got

initializing #1
initializing #2
init success
generating SQL Type
generated SQL type

/Users/tamizhvendan/TamizhWorks/temp/Rezoom.SQL/src/Rezoom.SQL.Provider/user.fsx(15,29): error FS3033: The type provider 'Rezoom.SQL.Provider.Provider' reported an error: Object reference not set to an instance of an object

I guess I am able to troubleshoot it using printfn. It would be helpful if you can help me in placing the print statements in the codebase to identify the root cause

rspeele commented 7 years ago

I haven't the slightest clue where that NRE might be coming from, but I think using this event handler to do some logging might help you obtain a useful stack trace:

https://msdn.microsoft.com/en-us/library/system.appdomain.firstchanceexception.aspx

tamizhvendan commented 7 years ago

Unfortunately, it didn't trap the exception!

Hi @ovatsus, @andrevdm, Can you folks help here? it is similar to the Fsharp.Data issue but we couldn't able to debug/troubleshoot it

tamizhvendan commented 7 years ago

@rspeele Is it because of the error being discussed in SO

rspeele commented 7 years ago

Leaving a note here that this might fix it:

https://github.com/fsprojects/FSharp.TypeProviders.StarterPack/issues/121

I'm on Windows only at the moment and not able to quickly repro the original issue so I can't tell if upgrading the ProvidedTypes.fs helps at all.

Swoorup commented 5 years ago

I tested on ubuntu with Mono 6.0.0 Preview and 5.20.1 Stable (5.20.1.19). Can confirm we still run into the same issue.