Browse Source

Merge pull request #26436 from abpframework/maliming/fix-telemetry-import-cycle

Fix stack overflow when `.abpmdl` modules import each other
pull/26452/head
Enis Necipoglu 1 week ago
committed by GitHub
parent
commit
dbda71cd5f
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 25
      framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/Activity/Providers/TelemetrySolutionInfoEnricher.cs
  2. 81
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/Telemetry/TelemetrySolutionInfoEnricher_Tests.cs

25
framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/Activity/Providers/TelemetrySolutionInfoEnricher.cs

@ -113,13 +113,32 @@ internal sealed class TelemetrySolutionInfoEnricher : TelemetryActivityEventEnri
continue;
}
ExtractModuleImportsInfo(modulePath, modules);
ExtractModuleImportsInfo(modulePath, modules, new HashSet<string>(StringComparer.Ordinal));
}
context.Current[ActivityPropertyNames.InstalledModules] = modules;
}
private static void ExtractModuleImportsInfo(string modulePath, List<Dictionary<string, object?>> modules)
private static void ExtractModuleImportsInfo(string modulePath, List<Dictionary<string, object?>> modules, HashSet<string> importChain)
{
// Modules can import each other, so skip a module that is already in the current import chain.
var fullModulePath = Path.GetFullPath(modulePath);
if (!importChain.Add(fullModulePath))
{
return;
}
try
{
ExtractModuleImportsInfoCore(modulePath, modules, importChain);
}
finally
{
importChain.Remove(fullModulePath);
}
}
private static void ExtractModuleImportsInfoCore(string modulePath, List<Dictionary<string, object?>> modules, HashSet<string> importChain)
{
var moduleJsonFileContent = File.ReadAllText(modulePath);
using var moduleDoc = JsonDocument.Parse(moduleJsonFileContent, new JsonDocumentOptions
@ -138,7 +157,7 @@ internal sealed class TelemetrySolutionInfoEnricher : TelemetryActivityEventEnri
if (!importPath.IsNullOrEmpty())
{
ExtractModuleImportsInfo(importPath, modules);
ExtractModuleImportsInfo(importPath, modules, importChain);
continue;
}

81
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Telemetry/TelemetrySolutionInfoEnricher_Tests.cs

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.Internal.Telemetry.Activity;
using Volo.Abp.Internal.Telemetry.Activity.Contracts;
using Volo.Abp.Internal.Telemetry.Activity.Providers;
using Volo.Abp.Internal.Telemetry.Constants;
using Xunit;
namespace Volo.Abp.Telemetry;
public class TelemetrySolutionInfoEnricher_Tests : IDisposable
{
private readonly string _solutionDirectory;
public TelemetrySolutionInfoEnricher_Tests()
{
_solutionDirectory = Path.Combine(Path.GetTempPath(), "abp-telemetry-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path.Combine(_solutionDirectory, "catalog"));
Directory.CreateDirectory(Path.Combine(_solutionDirectory, "ordering"));
}
[Fact]
public async Task Should_Handle_Modules_That_Import_Each_Other()
{
File.WriteAllText(Path.Combine(_solutionDirectory, "MySolution.abpsln"), """
{
"modules": {
"Catalog": { "path": "catalog/Catalog.abpmdl" },
"Ordering": { "path": "ordering/Ordering.abpmdl" }
}
}
""");
File.WriteAllText(Path.Combine(_solutionDirectory, "catalog", "Catalog.abpmdl"), """
{
"imports": {
"Volo.Abp.SettingManagement": { "version": "10.7.0" },
"Ordering": { "path": "../ordering/Ordering.abpmdl" }
}
}
""");
File.WriteAllText(Path.Combine(_solutionDirectory, "ordering", "Ordering.abpmdl"), """
{
"imports": {
"Volo.Abp.FeatureManagement": { "version": "10.7.0" },
"Catalog": { "path": "../catalog/Catalog.abpmdl" }
}
}
""");
var storage = Substitute.For<ITelemetryActivityStorage>();
storage.ShouldAddSolutionInformation(Arg.Any<Guid>()).Returns(true);
var enricher = new TelemetrySolutionInfoEnricher(storage, new ServiceCollection().BuildServiceProvider());
var context = ActivityContext.Create("Test");
context.Current[ActivityPropertyNames.SolutionId] = Guid.NewGuid();
context.ExtraProperties[ActivityPropertyNames.SolutionPath] = Path.Combine(_solutionDirectory, "MySolution.abpsln");
await enricher.EnrichAsync(context);
context.Current[ActivityPropertyNames.HasSolutionInfo].ShouldBe(true);
var moduleNames = ((List<Dictionary<string, object?>>)context.Current[ActivityPropertyNames.InstalledModules]!)
.Select(x => x[ActivityPropertyNames.ModuleName])
.ToList();
moduleNames.ShouldContain("Volo.Abp.SettingManagement");
moduleNames.ShouldContain("Volo.Abp.FeatureManagement");
}
public void Dispose()
{
if (Directory.Exists(_solutionDirectory))
{
Directory.Delete(_solutionDirectory, true);
}
}
}
Loading…
Cancel
Save