Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Clearscript V8 engine hang while debugging

0
0
Hi,

I am running Clearscript v8 script engine in debug mode to control script execution(Play,Pause,Stop). Clearscript V8 engine executing all scripts perfectly. I we put execution in a loop, after few script execution v8 will not write any response to newtork stream, it just hang.
//Clearscript  V8 script engine.
            var debuggingPort =2015;

            m_scriptEngine = new V8ScriptEngine(V8ScriptEngineFlags.DisableGlobalMembers | V8ScriptEngineFlags.EnableDebugging, debuggingPort)
            {
                AllowReflection = false,
            };
            Uri portUri = new Uri("tcp://127.0.0.1:"+debuggingPort);
            m_tcpClient = new TcpClient(portUri.Host, portUri.Port);
Send request to V8 Debug engine
      private Task<Response> SendRequestAsync(Request request, CancellationToken cancellationToken = new CancellationToken()) 
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (request.Sequence.HasValue == false)
                request.Sequence = m_currentSequence++;
            else
                m_currentSequence = request.Sequence.Value + 1;

             var requestResetEvent = new ManualResetEvent(false);
             _requestWaitHandles.AddOrUpdate(request.Sequence.Value, requestResetEvent, (id, r) => requestResetEvent);

             var serializedRequest = JsonConvert.SerializeObject(request);  
    //ex:- serializedRequest ={"seq":1,"type":"request","command":"setbreakpoint","arguments":{"type":"script","target":"1fa9ab33-fea8-41f5-91a4-d891e361a52f_IdeFfeVoLb.js [temp]","line":0}}

             return Task.Run(() => {

                 SendMessage(serializedRequest);

                 requestResetEvent.WaitOne();

                 Response response = null;
                 _responses.TryRemove(request.Sequence.Value, out response);

                 _requestWaitHandles.TryRemove(request.Sequence.Value, out requestResetEvent);

                 return response;
             });             
        }

        private async void SendMessage(string message) 
        {
            try
            {
               
                TcpClient networkClient;
                lock (m_networkClientLock)
                {
                    networkClient = m_tcpClient;
                }

                var networkStream = m_tcpClient.GetStream();

                var streamWriter = new StreamWriter(networkStream);
                
                int byteCount = Encoding.UTF8.GetByteCount(message);
                string request = string.Format("Content-Length: {0}{1}{1}{2}", byteCount, Environment.NewLine, message);

                if(Connected)
                {
                    await streamWriter.WriteAsync(request).ConfigureAwait(false);                    
                    await streamWriter.FlushAsync().ConfigureAwait(false);
                                       
                    WriteToLogFile("Written to stream. Message:" + request);                    
                }                
            }
            catch (Exception e)
            {
                WriteToLogFile("Exception writing to stream. Err Mesg:" + e.Message + Environment.NewLine);                    
                Debug.Write("Exception writing to stream. Err Mesg:" + e.Message+Environment.NewLine);
            }
Reading response from engine using while loop.
var networkStream=networkClient.GetStream();
                //networkStream.ReadTimeout = 2000;

                using (var streamReader = new StreamReader(networkStream, Encoding.Default))                              
                {                    
                   while (Connected) 
                   {
                       //if (networkStream.DataAvailable)
                       {
                           try
                           {
                               WriteToLogFile("Before Response: ");

                               var result = await streamReader.ReadLineAsync().ConfigureAwait(false);
                               //var result = streamReader.ReadLine();

                               WriteToLogFile("Response from engine. Response: " + result);

                               if (result == null)
                               {
                                   continue;
                               }

                               // Check whether result is content length header
                               var match = m_contentLength.Match(result);

                               if (!match.Success)
                               {
                                   //DebugWriteLine(string.Format("Debugger info: {0}", result));
                                   continue;
                               }

                               await streamReader.ReadLineAsync().ConfigureAwait(false);

                               // Retrieve content length
                               var length = int.Parse(match.Groups[1].Value);
                               if (length == 0)
                               {
                                   continue;
                               }

                               // Read content
                               string message = await streamReader.ReadLineBlockAsync(length).ConfigureAwait(false);

                               WriteToLogFile("Response from engine. Response: " + message);

                               HandleOutput(message);
                           }
                           catch (Exception exx)
                           {
                               WriteToLogFile("Exception reading from stream. Err Mesg:" + exx.Message + Environment.NewLine);
                           }
                       }
                    }
                }
Script hanging at line
var result = await streamReader.ReadLineAsync().ConfigureAwait(false);
No response from Clearscript V8 engine. step in request sent to engine on breakpoint event. Is there any other technique to control script execution.

Thanks and Regards
Praveen Kumar

Viewing all articles
Browse latest Browse all 2297