Menu

#55 When mails are processed in parallel filters can't read the content files

v1.0 (example)
closed
nobody
None
5
2023-11-12
2023-09-13
No

When E-Mails are processed in parallel, Filter Programs can't read the content file.
I have attached two .net 4.8 projects which demonstrate the problem. a ReadFilter which is a minimal filter wich reproduces the issue and a BulkMailSender which can be used to send mails in parrallel:

The used filter does nothing, but opening the file for a short moment (simulate reading) and sleeping (simulate processing):
ReadFilter Program.cs:

using System;
using System.IO;
using System.Threading;

var contentFilePath = Environment.GetCommandLineArgs()[1];

try
{
    using (var fileStream = File.Open(contentFilePath, FileMode.Open, FileAccess.Read))
    {
    }
}
catch (IOException ex)
{
    Console.WriteLine($"<<{ex.Message}>>");
    return 2;
}

Thread.Sleep(5000);

return 0;

Repro:
1. Install E-MailRelay as service 2.5 on Windows
2. Replace content of emailrelay-start.bat:

start "emailrelay" "C:\Program Files\E-MailRelay\emailrelay.exe" --close-stderr --filter C:\ProgramData\E-MailRelay\ReadFilter.exe --log --log-file C:\ProgramData\E-MailRelay\emailrelay-log-%%d.txt --log-time --pid-file C:\ProgramData\E-MailRelay\emailrelay.pid --spool-dir C:\ProgramData\E-MailRelay\spool --verbose
  1. Restart E-MailRelay service
  2. Build the ReadFilter Project and place the executable at C:\ProgramData\E-MailRelay\ReadFilter.exe
  3. Build and run the BulkMailSender project. Which sends two mails in parallel.

One mail will be accepted, and one mail will be rejected, because the filter can't access the content file.

emailrelay-log.txt:

emailrelay: 20230913.140522.830: info: rejected by filter: [The process cannot access the file 'C:\\ProgramData\\E-MailRelay\\spool\\emailrelay.6988.1694606722.22.content' because it is being used by another process.]
1 Attachments

Discussion

  • Bastian Schumacher

    The executables which are generated by the attached projects:

     
  • Graeme Walker

    Graeme Walker - 2023-09-13

    Thanks, I'll take a look later. The content file should be in the closed state when the filter runs (see src/gsmtp/gprotocolmessagestore.cpp(144) and src/gstore/gnewfile.cpp(89)).

     
  • Graeme Walker

    Graeme Walker - 2023-09-14

    The problem seems to be that the first message's filter inherits the file handle to the second message's content file and then .NET File.Open() in the second filter requires exclusive access. FWIW a filter using cscript and Scripting.FileSystemObject.OpenTextFile() works okay.

    I think the best fix would be to use UpdateProcThreadAttribute() with PROC_THREAD_ATTRIBUTE_HANDLE_LIST to limit the set of inherited file handles when doing CreateProcess.

     
  • Bastian Schumacher

    Thank you for your quick response. I assume, that you will fix this with your next release?

     
  • Graeme Walker

    Graeme Walker - 2023-09-17

    Before you get too hung up on VirusTotal try an experiment... Grab a Microsoft evaluation VM image from here:

    https://developer.microsoft.com/en-us/windows/downloads/virtual-machines/

    I used VirtualBox on Linux with 32Gb of memory but it still needed ~8Gb of swap and took forever to initialise, so try for a 64Gb host machine.

    Configure the VM with no network adaptor.

    Write a "hello world" C program ("notepad test.c"):

    #include <stdio.h>
    int main()
    {
        printf( "hello, world\n" ) ;
        return 0 ;
    }
    

    Build it from a "x64 native tools command prompt for vs 2022" shell using "cl test.c" There's no need to update VisualStudio. Then take a checksum with:

    certutil -hashfile test.exe sha256
    

    Submit "test.exe" to VirusTotal -- you can enable the network now that you have the SHA256 hash. Make sure the VirusTotal checksum matches the checksum you got earlier.

    I found the results enlightening. What do you get?

     
  • Graeme Walker

    Graeme Walker - 2023-11-12
    • status: open --> closed
     
  • Graeme Walker

    Graeme Walker - 2023-11-12

    Fixed in v2.5.1

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.