Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.IO.MemoryMappedFiles;
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
using Microsoft.Extensions.Logging;

namespace Azure.Functions.Cli.Actions.HostActions;

/// <summary>
/// A null implementation of MemoryMappedFileAccessor that disables shared memory functionality.
/// This can be used for testing or in environments where shared memory is not supported or desired.
/// </summary>
public class NullMemoryMappedFileAccessor : MemoryMappedFileAccessor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seal it to prevent unintended extension.

{
public NullMemoryMappedFileAccessor(ILogger<MemoryMappedFileAccessor> logger)
: base(logger)
{
Logger.LogDebug("Using NullMemoryMappedFileAccessor - shared memory data transfer is disabled");
}

public override bool TryCreate(string mapName, long size, out MemoryMappedFile mmf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a comment can help - "/// Always returns false; no shared memory is created. ///"

{
mmf = null;
Logger.LogDebug("NullMemoryMappedFileAccessor: TryCreate called for {MapName} with size {Size} - returning false", mapName, size);
return false;
}

public override bool TryOpen(string mapName, out MemoryMappedFile mmf)
{
mmf = null;
Logger.LogDebug("NullMemoryMappedFileAccessor: TryOpen called for {MapName} - returning false", mapName);
return false;
}

public override void Delete(string mapName, MemoryMappedFile mmf)
{
Logger.LogDebug("NullMemoryMappedFileAccessor: Delete called for {MapName} - no action taken", mapName);
mmf?.Dispose();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we care about mmf not being null or not already disposed?

}
}
5 changes: 5 additions & 0 deletions src/Cli/func/Actions/HostActions/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
using Microsoft.Azure.WebJobs.Script.WebHost.Authentication;
using Microsoft.Azure.WebJobs.Script.WebHost.Controllers;
using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection;
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand Down Expand Up @@ -84,6 +86,9 @@ public IServiceProvider ConfigureServices(IServiceCollection services)

services.AddWebJobsScriptHost(_builderContext.Configuration);

// Replace the default MemoryMappedFileAccessor with a null implementation
services.Replace(ServiceDescriptor.Singleton<IMemoryMappedFileAccessor, NullMemoryMappedFileAccessor>());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabiocav draft here; still need to consider all the potential impact of disabling this - let me know what you think

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think we still want to allow users to test this feature right? So maybe disable by default and add a flag to bring this back in. Or we just don't make a change here and fix it on the host side?


services.Configure<ScriptApplicationHostOptions>(o =>
{
o.ScriptPath = _hostOptions.ScriptPath;
Expand Down
Loading