Skip to content

Fix premature cleanup in code coverage with multiple IsMain processes #1111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 130 additions & 6 deletions dotnet/src/dotnetframework/GxClasses/Diagnostics/GXDebugManager.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
//#define _DEBUG_DEBUGGER
//#define _LOG_WRITER
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using GeneXus.Application;
using static GeneXus.Diagnostics.GXDebugStream;

namespace GeneXus.Diagnostics
{

public class GXDebugManager
{
internal const string _LOG_WRITER = "_LOG_WRITER";
public const short GXDEBUG_VERSION = 2;
internal const GXDebugGenId GENERATOR_ID = GXDebugGenId.CSHARP;
#if NETCORE
internal const GXDebugGenId GENERATOR_ID = GXDebugGenId.NET;
#else
internal const GXDebugGenId GENERATOR_ID = GXDebugGenId.NETFRAMEWORK;
#endif

internal const int PGM_INFO_NO_PARENT = 0;
internal static double MICRO_FREQ = Stopwatch.Frequency / 10e6D;
Expand Down Expand Up @@ -220,9 +228,25 @@ private void Save(GXDebugItem[] ToSave, int saveTop = -1, bool saveInThread = tr
else mSave(new object[] { ToSave, saveTop, saveCount });
}

public static void OnExit()
{
if(initialized)
{
Instance.OnExit(null);
}
}

internal void OnExit(GXDebugInfo dbgInfo)
{
PushSystem((int)GXDebugMsgCode.EXIT);
lock (saveLock)
{
for (int i = 0; i < dbgIndex; i++)
{
if (Current[i].Ticks == TICKS_NOT_SET)
Current[i].Ticks = TICKS_NOT_NEEDED;
}
}
Save();
}

Expand Down Expand Up @@ -292,12 +316,14 @@ private void mSave(object state1)
#if _DEBUG_DEBUGGER
Console.WriteLine("mSave-" + saveTop);
#endif
LogWriter.Append($"\n SaveTop = {saveTop}\n");
for (; idx < saveTop; idx++)
{
GXDebugItem dbgItem = Data[idx];
#if _DEBUG_DEBUGGER
Console.WriteLine($"item({idx}): { dbgItem }");
#endif
LogWriter.Log($"<{idx}:{dbgItem}>");
switch (dbgItem.MsgType)
{
case GXDebugMsgType.SYSTEM:
Expand Down Expand Up @@ -357,8 +383,11 @@ private void mSave(object state1)
continue;
}
ClearDebugItem(dbgItem);
LogWriter.LogPosition(stream);
}
LogWriter.Append($"\n ENDSAVE ");
}
LogWriter.Flush();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -421,8 +450,9 @@ internal enum GXDebugMsgCode : byte

internal enum GXDebugGenId : byte
{
CSHARP = 1,
NETFRAMEWORK = 1,
JAVA = 2,
NET = 3,

INVALID = 0xF
}
Expand Down Expand Up @@ -538,6 +568,7 @@ public enum ESCAPE : byte

public GXDebugStream(string FileName, FileMode fileMode) : base(FileName, fileMode, fileMode == FileMode.Open ? FileAccess.Read : (fileMode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.None)
{
LogWriter.SetLogName($"{FileName}.log");
Last = 0;
LastLast = 0;
InitializeNewBlock();
Expand Down Expand Up @@ -567,8 +598,16 @@ private void WriteEpilog()
}

public void Write(byte[] data) => Write(data, 0, data.Length);
public void WriteRaw(byte[] data, int from, int length) => base.Write(data, from, length);
public void WriteRaw(byte value) => base.WriteByte(value);
public void WriteRaw(byte[] data, int from, int length)
{
LogWriter.LogWriteRaw(data, from, length);
base.Write(data, from, length);
}
public void WriteRaw(byte value)
{
LogWriter.LogWriteByte(value);
base.WriteByte(value);
}

public override void Write(byte[] data, int offset, int count)
{
Expand All @@ -582,11 +621,13 @@ public override void Write(byte[] data, int offset, int count)
private byte Last, LastLast;
public override void WriteByte(byte value)
{
LogWriter.LogWriteByte(value);
base.WriteByte(value);
if (value == 0xFF &&
value == Last &&
value == LastLast)
{
LogWriter.Append("<3xFF> ");
WriteRaw(ESCAPE.TRIPLE_FF.ToByte());
Last = LastLast = 0;
}
Expand All @@ -602,6 +643,7 @@ public void WriteVLUInt(int value)
if (value < 0) throw new ArgumentException("Cannot handle negative values");
else if (value > 0x1FFFFFFFL)
throw new ArgumentException("Cannot handle > 29bit values");
LogWriter.LogWrite("VLUI", value);
if (value < 0x80)
WriteByte((byte)value);
else if (value < 0x4000)
Expand All @@ -616,6 +658,7 @@ public void WriteVLUInt(int value)
public void WriteVLUShort(short value)
{
if (value < 0) throw new ArgumentException("Cannot handle negative values");
LogWriter.LogWrite("VLUS", value);
if (value < 0x80)
WriteByte((byte)value);
else
Expand All @@ -627,6 +670,9 @@ public void WriteVLUShort(short value)

public void WriteHeader(Guid sessionGuid, short version, int saveCount)
{
LogWriter.Log("Header", ( "Guid", sessionGuid), ( "Version", version), ("SaveCount", saveCount));
LogWriter.LogPosition(this);

WriteProlog(version);
WriteVLUInt(saveCount);
Write(sessionGuid.ToByteArray());
Expand Down Expand Up @@ -689,6 +735,8 @@ internal void WritePgmTrace(int SId, int line1, int col, long ticks)
public void WriteScaledLong(long N)
{
if (N < 0) throw new ArgumentException("Cannot handle negative values");
LogWriter.LogWrite("SCAL", N);

int m = 0;
while (N > 31)
{
Expand Down Expand Up @@ -874,6 +922,82 @@ public void InitializeNewBlock()
LastSId = 0;
LastLine1 = 0;
}

internal class LogWriter
{
private static string LogWriterName = "GXDebugCoverage.log";
public static object LogWriterLock = new object();
private static StringBuilder m_Buffer = new StringBuilder();

[Conditional(GXDebugManager._LOG_WRITER)]
public static void SetLogName(string name)
{
LogWriterName = name;
}

[Conditional(GXDebugManager._LOG_WRITER)]
public static void LogPosition(GXDebugStream stream)
{
Append($"\nPos: {stream.Position}\n");
}

[Conditional(GXDebugManager._LOG_WRITER)]
public static void Append(string msg)
{
lock (LogWriterLock)
{
m_Buffer.Append(msg);
}
}

[Conditional(GXDebugManager._LOG_WRITER)]
public static void Flush()
{
try
{
lock (LogWriterLock)
{
File.AppendAllText(LogWriterName, m_Buffer.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
m_Buffer.Clear();
}

[Conditional(GXDebugManager._LOG_WRITER)]
internal static void Log(string title, params (string key, object value)[] values)
{
StringBuilder sb = new StringBuilder();
Append($"=========================================\n{title}\n");
if (values != null)
{
foreach ((string key, object value) in values)
{
sb.Append($" {key} = {value?.ToString()}\n");
}
}
Append(sb.ToString());
}

[Conditional(GXDebugManager._LOG_WRITER)]
internal static void LogWrite(string type, long value) => Append($"{type}:{value} ");

[Conditional(GXDebugManager._LOG_WRITER)]
internal static void LogWriteByte(byte value) => Append($"({value.ToString("X2")}) ");

[Conditional(GXDebugManager._LOG_WRITER)]
internal static void LogWriteRaw(byte[] data, int from, int length)
{
lock (LogWriterLock)
{
for (int i = 0; i < length; i++)
m_Buffer.Append($"({data[from+i].ToString("X2")}) ");
}
}
}
}

#if _DEBUG_DEBUGGER
Expand All @@ -897,7 +1021,7 @@ public GXDebugReader(string FileName)
this.FileName = FileName;
}

public static int Main(String[] args)
public static int Main(string[] args)
{
try
{
Expand Down
7 changes: 6 additions & 1 deletion dotnet/src/dotnetframework/GxClasses/Model/gxproc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,22 @@ protected void exitApplication()
}
private void exitApplication(bool flushBatchCursor)
{
bool inMainExit = false;
if (!(GxContext.IsHttpContext || GxContext.IsRestService) && IsMain && GxApplication.MainContext==context)
{
ThreadUtil.WaitForEnd();
inMainExit = true;
}

if (flushBatchCursor)
{
foreach (IGxDataStore ds in context.DataStores)
ds.Connection.FlushBatchCursors(this);
}

if (IsMain)
if (inMainExit)
dbgInfo?.OnExit();

if (disconnectUserAtCleanup)
{
try
Expand Down
Loading