microsoft / dev-proxy

Dev Proxy is an API simulator that helps you effortlessly test your app beyond the happy path.
https://aka.ms/devproxy
MIT License
471 stars 56 forks source link

[BUG]: DevToolsPlugin returns the error Unable to enumerate the process modules #840

Closed qmatteoq closed 1 month ago

qmatteoq commented 1 month ago

Description

When you enable the DevToolsPlugin and you run devproxy, the execution fails with an exception

Expected behaviour

The browser configured in the devproxyrc.json file starts up with the Developer Tools turned on, showing the requests intercepted by Dev Proxy

Actual behaviour

DevProxy fails to launch with the following exception

Unhandled exception. System.ComponentModel.Win32Exception (0x80004005): Unable to enumerate the process modules.
   at System.Diagnostics.NtProcessManager.GetModules(Int32 processId, Boolean firstModuleOnly)
   at System.Diagnostics.NtProcessManager.GetFirstModule(Int32 processId)
   at Microsoft.DevProxy.Plugins.Inspection.DevToolsPlugin.<>c__DisplayClass10_0.<GetBrowserProcesses>b__0(Process p)
   at System.Linq.Enumerable.WhereArrayIterator`1.ToArray()
   at Microsoft.DevProxy.Plugins.Inspection.DevToolsPlugin.GetBrowserProcesses(String browserPath)
   at Microsoft.DevProxy.Plugins.Inspection.DevToolsPlugin.InitInspector()
   at Microsoft.DevProxy.Plugins.Inspection.DevToolsPlugin.Register()
   at Microsoft.DevProxy.PluginLoader.LoadPlugins(IPluginEvents pluginEvents, IProxyContext proxyContext)
   at Program.<Main>$(String[] args)
   at Program.<Main>(String[] args)

Steps to reproduce

1) Open devproxyrc.json and add the DevToolsPlugin in the plugins section:

{
  "name": "DevToolsPlugin",
  "enabled": true,
  "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
  "configSection": "devTools"
}

2) At the end of the file, add a section to define the configuration:

"devTools": {
  "preferredBrowser": "Edge"
}

3) Launch DevProxy 4) Observe the exception being triggered

Dev Proxy Version

0.19.1

Operating system (environment)

Windows

Shell

PowerShell

Configuration file

{
  "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.1/rc.schema.json",
  "plugins": [
    {
      "name": "ExecutionSummaryPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "executionSummaryPlugin"
    },
    {
      "name": "GraphMockResponsePlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "graphConnectorMocks"
    },
    {
      "name": "DevToolsPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "devTools"
    }
  ],
  "urlsToWatch": [
    "https://graph.microsoft.com/v1.0/*",
    "https://graph.microsoft.com/beta/*",
    "https://graph.microsoft.us/v1.0/*",
    "https://graph.microsoft.us/beta/*",
    "https://dod-graph.microsoft.us/v1.0/*",
    "https://dod-graph.microsoft.us/beta/*",
    "https://microsoftgraph.chinacloudapi.cn/v1.0/*",
    "https://microsoftgraph.chinacloudapi.cn/beta/*"
  ],
  "graphConnectorMocks": {
    "mocksFile": ".devproxy/graph-connector-mocks.json" 
  },
  "executionSummaryPlugin": {
      "groupBy": "url"
  },
  "devTools": {
    "preferredBrowser": "Edge"
  }
}

Additional Info

The issue is happening in the following code of the DevToolsPlugin.cs file. The problem seems to be with the method which enumerates the running processes.

    private Process[] GetBrowserProcesses(string browserPath)
    {
        return Process.GetProcesses().Where(p =>
            p.MainModule is not null && p.MainModule.FileName == browserPath
        ).ToArray();
    }
waldekmastykarz commented 1 month ago

Thanks for reporting the issue @qmatteoq. Let's dig into it and try to understand why this is happening and how to best fix it.