sass / libsass-net

A lightweight wrapper around libsass
MIT License
94 stars 35 forks source link

Intermittent NullReferenceExceptions #66

Open Seltzer opened 6 years ago

Seltzer commented 6 years ago

Hi!

I'm using libsass-net to compile a top level SCSS file which imports about 20 ancestor files (code attached). I'm seeing intermittent NullReferenceExceptions maybe once every 5-10 compilations and the point of failure varies (i.e. the import after which it dies). Here's the stack trace:

System.NullReferenceException: Object reference not set to an instance of an object. at LibSass.Compiler.SassExterns64.sass_compile_data_context(SassSafeDataContextHandle data_context) at LibSass.Compiler.Context.SassSafeDataContextHandle.CompileInternalContext() at LibSass.Compiler.Context.SassSafeContextHandle.CompileContext()

Given this, I assume it's something going wrong inside libsass itself. I can reliably reproduce this by periodically making requests to our endpoint which compiles that top level file. I would guess that it's unrelated to concurrency as I'm waiting a few seconds in between. However, it's quite possible that libsass is being dodgy and sharing some state between compilations.

I attempted to debug it and track it down by building libsass 3.3.6 from source with logging statements attached. Didn't have much luck though. This is possibly the same issue as https://github.com/sass/libsass-net/issues/60.

I haven't lodged a bug report with libsass as I'm unsure whether it's still a bug in their latest version. How much effort do you think would be required to bring libsass-net up to date so that it works with 3.5.4? I might end up giving it a shot.

var compilerOptions = new SassOptions { Data = topLevelScss, EmbedSourceMap = true, OutputStyle = SassOutputStyle.Expanded, Importers = new[] { BuildImportResolver(scssResources) }, };

var sassCompiler = new SassCompiler(compilerOptions); var compilationResult = sassCompiler.Compile();

Seltzer commented 5 years ago

Hi @darrenkopp

I'm just wondering, how much effort do you think would be required if I were to attempt to make libsass-net work with the latest version of libsass? Is there anything I should watch out for in particular?

Cheers

darrenkopp commented 5 years ago

I don't think there's anything to watch out for. The biggest hurdle is likely going to be getting it to compile on windows, then it's a simple matter of mapping P/Invoke commands for anything that's changed (if anything)

SorenHK commented 5 years ago

I am facing the samme issue with the exact same exception as @Seltzer (NullReferenceException). I've figured out that the actual exception is:

'CallbackOnCollectedDelegate' : 'A callback was made on a garbage collected delegate of type 'LibSass.NET!LibSass.Compiler.SassImporterDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.'

I have, however, managed to "handle" it by simply retrying the compilation when this exception is thrown. In my testing the exception was thrown, on average, every 300 compilations. Retrying with up to a few attempts seems reasonable until a permanent fix is implemented. I have yet to see two consecutive failed compilations in a row.