<commit_before>// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.AspNet.Server.Kestrel.Networking
{
    /// <summary>
    /// Summary description for UvShutdownRequest
    /// </summary>
    public class UvShutdownReq : UvReq
    {
        private readonly static Libuv.uv_shutdown_cb _uv_shutdown_cb = UvShutdownCb;

        Action<UvShutdownReq, int, object> _callback;
        object _state;

        public void Init(UvLoopHandle loop)
        {
            CreateMemory(
                loop.Libuv, 
                loop.ThreadId,
                loop.Libuv.req_size(Libuv.RequestType.SHUTDOWN));
        }

        public void Shutdown(UvStreamHandle handle, Action<UvShutdownReq, int, object> callback, object state)
        {
            _callback = callback;
            _state = state;
            _uv.shutdown(this, handle, _uv_shutdown_cb);
        }

        private static void UvShutdownCb(IntPtr ptr, int status)
        {
            var req = FromIntPtr<UvShutdownReq>(ptr);
            req._callback(req, status, req._state);
            req._callback = null;
            req._state = null;
        }
    }
}<commit_msg>Fix NPE issue where UvShutdownReq being garbage collected before the shutdown callback executed<commit_after>// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Runtime.InteropServices;

namespace Microsoft.AspNet.Server.Kestrel.Networking
{
    /// <summary>
    /// Summary description for UvShutdownRequest
    /// </summary>
    public class UvShutdownReq : UvReq
    {
        private readonly static Libuv.uv_shutdown_cb _uv_shutdown_cb = UvShutdownCb;

        Action<UvShutdownReq, int, object> _callback;
        object _state;
        GCHandle _pin;

        public void Init(UvLoopHandle loop)
        {
            CreateMemory(
                loop.Libuv, 
                loop.ThreadId,
                loop.Libuv.req_size(Libuv.RequestType.SHUTDOWN));
        }

        public void Shutdown(UvStreamHandle handle, Action<UvShutdownReq, int, object> callback, object state)
        {
            _callback = callback;
            _state = state;
            _pin = GCHandle.Alloc(this, GCHandleType.Normal);
            _uv.shutdown(this, handle, _uv_shutdown_cb);
        }

        private static void UvShutdownCb(IntPtr ptr, int status)
        {
            var req = FromIntPtr<UvShutdownReq>(ptr);
            req._pin.Free();
            req._callback(req, status, req._state);
            req._callback = null;
            req._state = null;
        }
    }
}
{'fraction_non_alphanumeric': 0.08445706174591909, 'fraction_numerical': 0.0014194464158978, 'mean_word_length': 2.132222222222222, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1462391', 'n_tokens_mistral': 814, 'n_tokens_neox': 795, 'n_words': 204}