Skip to content

Commit f971ff6

Browse files
authored
Use genuine file descriptors on Posix (#1832)
* Use genuine file descriptors on Unix * Complete implementing genuine file descriptors * Add developer's deocumentation * Update after review * Add wbplus file test
1 parent 517c548 commit f971ff6

File tree

6 files changed

+315
-64
lines changed

6 files changed

+315
-64
lines changed

Documentation/file-descriptors.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# File Descriptors in IronPython
2+
3+
## Windows
4+
5+
The conceptual picture of file descriptors (FDs) usage on Windows, for the most interesting case of `FileStream`:
6+
7+
```mermaid
8+
graph LR;
9+
10+
FileIO --> StreamBox --> FileStream --> Handle(Handle) --> OSFile[OS File];
11+
FD(FD) <--> StreamBox;
12+
```
13+
14+
Conceptually, the relationship between `FD` (a number) and `StreamBox` (a class) is bidirectional because `PythonFileManager` (global singleton) maintains the association between the two so it is cost-free to obtaining the one having the other. FD is not the same as the handle, which is created by the OS. FD is an emulated (fake) file descriptor, assigned by the `PythonFileManager`, for the purpose of supporting the Python API. The descriptors are allocated lazily, i.e. only if the user code makes an API call that accesses it. Once assigned, the descriptor does not change. The FD number is released once the FD is closed (or the associated `FileIO` is closed and had `closefd` set to true.)
15+
16+
It is possible to have the structure above without `FileIO`; for instance when an OS file is opened with one of the low-level functions in `os`, or when an existing FD is duplicated. It is also possible to associate an FD with several `FileIO`. In such cases it is the responsibility of the user code to take care that the FD is closed at the right time.
17+
18+
When FD is duplicated (using `dup` or `dup2`), the associated `StreamBox` is duplicated (there is always a 1-to-1 relationship between FD and `StreamBox`), but the underlying `FileStream` objects remain the same, and so are the underlying OS handles. The new FD may be used to create a `FileIO` (or several, just as the original FD). All read/seek/write operations on both descriptors go though the same `FileStream` object and the same OS handle.
19+
20+
```mermaid
21+
graph LR;
22+
23+
FD1(FD1) <--> StreamBox --> FileStream --> Handle(Handle) --> OSFile[OS File];
24+
FD2(FD2) <--> StreamBox2[StreamBox] --> FileStream;
25+
```
26+
27+
The descriptors can be closed independently, and the underlying `FileStream` is closed when the last `StreamBox` using it is closed.
28+
29+
## Posix
30+
31+
On Unix-like systems (Linux, maxOS), `FileStream` uses the actual file descriptor as the handle. In the past. IronPython was ignoring this and still issuing its own fake file descriptors as it is in the case of Windows. Now, however, the genuine FD is extracted from the handle and used as FD at the `PythonFileManager` level, ensuring that clients of Python API obtain the genuine FD.
32+
33+
```mermaid
34+
graph LR;
35+
36+
FileIO --> StreamBox --> FileStream --> FDH(FD) --> OSFile[OS File];
37+
FD(FD) <--> StreamBox;
38+
```
39+
40+
When descriptor FD is duplicated, the actual OS call is made to create the duplicate FD2. In order to use FD2 directly, a new `Stream` object has to be created around it.
41+
42+
### Optimal Mechanism
43+
44+
The optimal solution is to create another `FileStream` using the constructor that accepts an already opened file descriptor.
45+
46+
```mermaid
47+
graph LR;
48+
49+
FD1(FD1) <--> StreamBox --> FileStream --> FDH1(FD1) --> OSFile[OS File];
50+
FD2(FD2) <--> StreamBox2[StreamBox] --> FileStream2[FileStream] --> FDH2(FD2) --> OSFile;
51+
```
52+
53+
In this way, the file descriptor on the `PythonFileManager` level is the same as the file descriptor used by `FileStream`.
54+
55+
Unfortunately, on .NET, somehow, two `FileStream` instances using the same file descriptor will have the two independent read/write positions. This is not how duplicated file descriptors should work: both descriptors should point to the same file description structure and share the read/seek/write position. In practice, on .NET, writing through the second file object will overwrite data already written through the first file object. In regular Unix applications (incl. CPython), the subsequent writes append data, regardless which file object is used. The same principle should apply to reads.
56+
57+
Also unfortunately, on Mono, the `FileStream` constructor accepts only descriptors opened by another call to a `FileStream` constructor[[1]]. So descriptors obtained from direct OS calls, like `open`, `creat`, `dup`, `dup2` are being rejected.
58+
59+
### Mono Workaround
60+
61+
To use system-opened file descriptors on Mono `UnixStream` can be used instead of `FileStream`.
62+
63+
```mermaid
64+
graph LR;
65+
66+
FD1(FD1) <--> StreamBox --> FileStream --> FDH1(FD1) --> OSFile[OS File];
67+
FD2(FD2) <--> StreamBox2[StreamBox] --> UnixStream --> FDH2(FD2) --> OSFile;
68+
```
69+
70+
Since FileIO works with various types of the underlying `Stream`, using `UnixStream` should be OK.
71+
72+
Although `UnixStream` is available in .NET through package `Mono.Posix`, this solution still does not work around desynchronized read/write position, which `FileStream` using the original FD1 must somehow maintain independently.
73+
74+
### .NET Workaround
75+
76+
To ensure proper R/W behavior on .NET, operations on both file descriptions have to go though the same `FileStream` object. Since the duplicated file descriptor is basically just a number, pointing to the same file description as the original descriptor, on the OS level it doesn't matter which descriptor is used for operations. The only difference between those descriptors is flag `O_CLOEXEC`, which determines whether the descriptor stays open or not when child processed are executed.
77+
78+
```mermaid
79+
graph LR;
80+
81+
FD1(FD1) <--> StreamBox --> FileStream --> FDH1(FD1) --> OSFile[OS File];
82+
FD2(FD2) <--> StreamBox2[StreamBox] --> FileStream;
83+
FDH2(FD2) --> OSFile;
84+
```
85+
86+
This actually works OK, until `dup2` is used. When the FD1 descriptor (or the associated `FileIO`) is closed on the Python API level, the underlying OS descriptor is not released but still being used by `FileStream`. A small side effect is that it will not be reused until FD2 is closed, but other than that, the behaviour is as expected.
87+
88+
```mermaid
89+
graph LR;
90+
91+
FileStream --> FDH1(FD1) --> OSFile[OS File];
92+
FD2(FD2) <--> StreamBox2[StreamBox] --> FileStream;
93+
FDH2(FD2) --> OSFile;
94+
```
95+
96+
The problem arises when `dup2` is used with the target being FD1. This will forcibly close the descriptor used by `FileStream`, rendering the stream broken, despite having FD2 available. Perhaps closing `FileStream` using FD1 and opening a replacement around FD2 could be a solution, but this would have to be done atomically. If so, this would lead to a healthy structure.
97+
98+
```mermaid
99+
graph LR;
100+
101+
FileStream --> FDH2(FD2);
102+
FD2(FD2) <--> StreamBox2[StreamBox] --> FileStream;
103+
FDH2(FD2) --> OSFile;
104+
```
105+
106+
107+
## Practical Scenarios
108+
109+
None of the above solutions is fully satisfactory for .NET. Ideally, .NET would behave consistently with Posix, because even the most elaborate workarounds (like juggling various `FileStream` objects around the descriptors) only work within IronPython, and break down when a descriptor is passed to a 3rd party library that uses C extension and creates its own `FILE*` struct around it. The `FileStream` object in .NET knows nothing about it and will not adjust its R/W position.
110+
111+
In the meantime, let's look at some practical cases when `dup`/`dup2` are used and try to support just these. For what I have seen, `dup`/`dup2` are commonly used to redirect some of the standard descriptors. For example, to redirect standard output to a file:
112+
1. Open a file for writing, it will get assigned descriptor FD1.
113+
2. Copy descriptor 1 aside using `dup`. The copy will get assigned descriptor FD2.
114+
3. Copy the open file descriptor FD1 onto descriptor 1 using `dup2`. This will forcibly close the existing descriptor 1, but not the output stream, which is sill accessible through descriptor FD2.
115+
4. Code writing to "standard output", i.e. descriptor 1, will now write to the open file.
116+
5. If needed, the application can still write to the original output stream by writing to descriptor FD2.
117+
6. When done, close descriptor FD1.
118+
7. Copy descriptor FD2 onto descriptor 1 using `dup2`. Since the is the last one pointing to the open file, the file will be closed as well.
119+
8. Close descriptor FD2, the copy is not needed anymore.
120+
121+
The same scenario is commonly done for standard input and sometimes standard error.
122+
123+
The problem of .NET manifests itself when there are two descriptors open that refer to the same open file description and used concurrently. In the above scenario it is descriptor 1 and FD1. Assuming that the application is not using FD1 (typical use), the _Optimal Mechanism_ described above is sufficient.
124+
125+
If the application does insist on using both descriptors 1 and FD1, the first .NET workaround is needed. This will lead to the following structure:
126+
127+
```mermaid
128+
graph LR;
129+
130+
FD1(FD1) <--> StreamBox --> FileStream --> FDH1(FD1) --> OSFile[OS File];
131+
D1(1) <--> StreamBox2[StreamBox] --> FileStream;
132+
DH1(1) --> OSFile;
133+
FD2(FD2) <--> StreamBox3[StreamBox] --> FileStream2[FileStream] --> FDH2(FD2) --> stdout
134+
```
135+
136+
The problem of closing FD1 and then overwriting it is not an issue, since only standard descriptors (0, 1, 2) are being overwritten with `dup2`. There is still a problem of overwriting data written by C extension code writing though descriptor 1. Perhaps replacing `FileStream` utilizing FD1 with `UnixStream` from Mono would make it more cooperative.
137+
138+
In the end, the implementation of genuine file descriptors in IronPython starts with the simple solution (the simple workarounds described above) and will be adjusted as needed to support the 3rd party Python packages.
139+
140+
## Special Case: Double Stream
141+
142+
In Python, a file can be opened with mode "ab+". The file is opened for appending to the end (created if not exists), and the `+` means that it is also opened for updating. i.e. reading and writing. The file pointer is initially set at the end of the file (ready to write to append) but can be moved around to read already existing data. However, each write will append data to the end and reset the read/write pointer at the end again. In IronPython this is simulated by using two file streams, one for reading and one fore writing. Both are maintained in a single `StreamBox` but will have different file descriptors. This is subject to change.
143+
144+
[1]: https://github.com/mono/mono/issues/12783

Src/IronPython.Modules/nt.cs

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,25 @@ public static int dup(CodeContext/*!*/ context, int fd) {
351351
PythonFileManager fileManager = context.LanguageContext.FileManager;
352352

353353
StreamBox streams = fileManager.GetStreams(fd); // OSError if fd not valid
354-
fileManager.EnsureRefStreams(streams);
355-
fileManager.AddRefStreams(streams);
356-
return fileManager.Add(new(streams));
354+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
355+
if (!streams.IsSingleStream && fd is 1 or 2) {
356+
// If there is a separate write stream, dupping over stout or sderr uses write stream's file descriptor
357+
fd = streams.WriteStream is FileStream fs ? fs.SafeFileHandle.DangerousGetHandle().ToInt32() : fd;
358+
}
359+
int fd2 = UnixDup(fd, -1, out Stream? dupstream);
360+
if (dupstream is not null) {
361+
return fileManager.Add(fd2, new(dupstream));
362+
} else {
363+
// Share the same set of streams between the original and the dupped descriptor
364+
fileManager.EnsureRefStreams(streams);
365+
fileManager.AddRefStreams(streams);
366+
return fileManager.Add(fd2, new(streams));
367+
}
368+
} else {
369+
fileManager.EnsureRefStreams(streams);
370+
fileManager.AddRefStreams(streams);
371+
return fileManager.Add(new(streams));
372+
}
357373
}
358374

359375

@@ -373,11 +389,45 @@ public static int dup2(CodeContext/*!*/ context, int fd, int fd2) {
373389
close(context, fd2);
374390
}
375391

376-
// TODO: race condition: `open` or `dup` on another thread may occupy fd2
392+
// TODO: race condition: `open` or `dup` on another thread may occupy fd2 (simulated descriptors only)
393+
394+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
395+
fileManager.EnsureRefStreams(streams);
396+
fileManager.AddRefStreams(streams);
397+
return fileManager.Add(fd2, new(streams));
398+
} else {
399+
if (!streams.IsSingleStream && fd is 1 or 2) {
400+
// If there is a separate write stream, dupping over stout or sderr uses write stream's file descriptor
401+
fd = streams.WriteStream is FileStream fs ? fs.SafeFileHandle.DangerousGetHandle().ToInt32() : fd;
402+
}
403+
fd2 = UnixDup(fd, fd2, out Stream? dupstream); // closes fd2 atomically if reopened in the meantime
404+
fileManager.Remove(fd2);
405+
if (dupstream is not null) {
406+
return fileManager.Add(fd2, new(dupstream));
407+
} else {
408+
// Share the same set of streams between the original and the dupped descriptor
409+
fileManager.EnsureRefStreams(streams);
410+
fileManager.AddRefStreams(streams);
411+
return fileManager.Add(fd2, new(streams));
412+
}
413+
}
414+
}
415+
377416

378-
fileManager.EnsureRefStreams(streams);
379-
fileManager.AddRefStreams(streams);
380-
return fileManager.Add(fd2, new(streams));
417+
private static int UnixDup(int fd, int fd2, out Stream? stream) {
418+
int res = fd2 < 0 ? Mono.Unix.Native.Syscall.dup(fd) : Mono.Unix.Native.Syscall.dup2(fd, fd2);
419+
if (res < 0) throw GetLastUnixError();
420+
if (ClrModule.IsMono) {
421+
// This does not work on .NET, probably because .NET FileStream is not aware of Mono.Unix.UnixStream
422+
stream = new Mono.Unix.UnixStream(res, ownsHandle: true);
423+
} else {
424+
// This does not work 100% correctly on .NET, probably because each FileStream has its own read/write cursor
425+
// (it should be shared between dupped descriptors)
426+
//stream = new FileStream(new SafeFileHandle((IntPtr)res, ownsHandle: true), FileAccess.ReadWrite);
427+
// Accidentaly, this would also not work on Mono: https://github.com/mono/mono/issues/12783
428+
stream = null; // Handle stream sharing in PythonFileManager
429+
}
430+
return res;
381431
}
382432

383433
#if FEATURE_PROCESS
@@ -827,22 +877,28 @@ public static object open(CodeContext/*!*/ context, [NotNone] string path, int f
827877
FileMode fileMode = FileModeFromFlags(flags);
828878
FileAccess access = FileAccessFromFlags(flags);
829879
FileOptions options = FileOptionsFromFlags(flags);
830-
Stream fs;
880+
Stream s;
881+
FileStream? fs;
831882
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && IsNulFile(path)) {
832-
fs = Stream.Null;
883+
fs = null;
884+
s = Stream.Null;
833885
} else if (access == FileAccess.Read && (fileMode == FileMode.CreateNew || fileMode == FileMode.Create || fileMode == FileMode.Append)) {
834886
// .NET doesn't allow Create/CreateNew w/ access == Read, so create the file, then close it, then
835887
// open it again w/ just read access.
836888
fs = new FileStream(path, fileMode, FileAccess.Write, FileShare.None);
837889
fs.Close();
838-
fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, DefaultBufferSize, options);
890+
s = fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, DefaultBufferSize, options);
839891
} else if (access == FileAccess.ReadWrite && fileMode == FileMode.Append) {
840-
fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, DefaultBufferSize, options);
892+
s = fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, DefaultBufferSize, options);
841893
} else {
842-
fs = new FileStream(path, fileMode, access, FileShare.ReadWrite, DefaultBufferSize, options);
894+
s = fs = new FileStream(path, fileMode, access, FileShare.ReadWrite, DefaultBufferSize, options);
843895
}
844896

845-
return context.LanguageContext.FileManager.Add(new(fs));
897+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
898+
return context.LanguageContext.FileManager.Add(new(s));
899+
} else {
900+
return context.LanguageContext.FileManager.Add((int)fs!.SafeFileHandle.DangerousGetHandle(), new(s));
901+
}
846902
} catch (Exception e) {
847903
throw ToPythonException(e, path);
848904
}
@@ -877,30 +933,29 @@ private static FileOptions FileOptionsFromFlags(int flag) {
877933

878934
#if FEATURE_PIPES
879935

880-
private static Tuple<Stream, Stream> CreatePipeStreams() {
881-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
882-
return CreatePipeStreamsUnix();
883-
} else {
936+
public static PythonTuple pipe(CodeContext context) {
937+
var manager = context.LanguageContext.FileManager;
938+
939+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
884940
var inPipe = new AnonymousPipeServerStream(PipeDirection.In);
885941
var outPipe = new AnonymousPipeClientStream(PipeDirection.Out, inPipe.ClientSafePipeHandle);
886-
return Tuple.Create<Stream, Stream>(inPipe, outPipe);
942+
return PythonTuple.MakeTuple(
943+
manager.Add(new(inPipe)),
944+
manager.Add(new(outPipe))
945+
);
946+
} else {
947+
var pipeStreams = CreatePipeStreamsUnix();
948+
return PythonTuple.MakeTuple(
949+
manager.Add(pipeStreams.Item1, new(pipeStreams.Item2)),
950+
manager.Add(pipeStreams.Item3, new(pipeStreams.Item4))
951+
);
887952
}
888953

889-
static Tuple<Stream, Stream> CreatePipeStreamsUnix() {
954+
static Tuple<int, Stream, int, Stream> CreatePipeStreamsUnix() {
890955
Mono.Unix.UnixPipes pipes = Mono.Unix.UnixPipes.CreatePipes();
891-
return Tuple.Create<Stream, Stream>(pipes.Reading, pipes.Writing);
956+
return Tuple.Create<int, Stream, int, Stream>(pipes.Reading.Handle, pipes.Reading, pipes.Writing.Handle, pipes.Writing);
892957
}
893958
}
894-
895-
public static PythonTuple pipe(CodeContext context) {
896-
var pipeStreams = CreatePipeStreams();
897-
var manager = context.LanguageContext.FileManager;
898-
899-
return PythonTuple.MakeTuple(
900-
manager.Add(new(pipeStreams.Item1)),
901-
manager.Add(new(pipeStreams.Item2))
902-
);
903-
}
904959
#endif
905960

906961
#if FEATURE_PROCESS

Src/IronPython/Modules/_fileio.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static Exception BadMode(string mode) {
239239

240240
[Documentation("close() -> None. Close the file.\n\n"
241241
+ "A closed file cannot be used for further I/O operations. close() may be"
242-
+ "called more than once without error. Changes the fileno to -1."
242+
+ "called more than once without error."
243243
)]
244244
public override void close(CodeContext/*!*/ context) {
245245
if (_closed) {

0 commit comments

Comments
 (0)