Sunday, September 8, 2024

Is it safe to send on one thread and recv on another thread on the same socket

POSIX: yes

2.9.1 Thread-Safety

All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the following functions need not be thread-safe.

Source: https://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_09.html#tag_02_09_01

Windows: also yes

3.10 - Is Winsock thread-safe?

On modern Windows stacks, yes, it is, within limits.

It is safe, for instance, to have one thread calling send() and another thread calling recv() on a single socket.

By contrast, it’s a bad idea for two threads to both be calling send() on a single socket. This is “thread-safe” in the limited sense that your program shouldn’t crash, and you certainly shouldn’t be able to crash the kernel, which is handling these send() calls. The fact that it is “safe” doesn’t answer key questions about the actual effect of doing this. Which call’s data goes out first on the connection? Does it get interleaved somehow? Don’t do this.

Instead of multiple threads accessing a single socket, you may want to consider setting up a pair of network I/O queues. Then, give one thread sole ownership of the socket: this thread sends data from one I/O queue and enqueues received data on the other. Then other threads can access the queues (with suitable synchronization).

Applications that use some kind of non-synchronous socket typically have some I/O queue already. Of particular interest in this case is overlapped I/O or I/O completion ports, because these I/O strategies are also thread-friendly. You can tell Winsock about several OVERLAPPED blocks, and Winsock will finish sending one before it moves on to the next. This means you can keep a chain of these OVERLAPPED blocks, each perhaps added to the chain by a different thread. Each thread can also call WSASend() on the block they added, making your main loop simpler.

Source: https://tangentsoft.com/wskfaq/intermediate.html#threadsafety

Source: https://cboard.cprogramming.com/c-programming/150774-parallel-threads-socket-send-recv.html


Go vs Python Part 1

I want to be able to send and recv on a socket at the same time. 

Go has a nice feature where you can select on channels.

Python does not allow you to do this. 

In fact, Windows Python does not allow you to select on anything except sockets.

Fortunately since Python 3.5, socketpair is available.

Using socketpair you can do a hacky workaround: https://stackoverflow.com/questions/51104534/python-socket-receive-send-multi-threading

You create an extra socket and then you do select on that extra socket (for send) along with the "main" socket (for recv).

But since syscalls are expensive you only since a single byte on that socket to notify the other thread to process the queue.

Anyway I thought this was a pretty inelegant workaround for what is basically a missing language feature.

If Python allowed you to do select on queues like Go allows you to select on channels then this hacky workaround would not be required.

Stupidly bought an enterprise NVME now my Intel NUC consumes 6W in sleep mode

Normally my Intel NUC consumes less than 1W in sleep mode. 

But I stupidly bought and installed a Micron 7450 MAX in my NUC.

Now when my NUC is in sleep mode it consumes 6W. And when awake and in idle it consumes 10W instead of the 4W that it usually consumes.

I updated the SSD and the BIOS firmware to latest versions and the power consumption GOT WORSE.

Fuck.

All I want is a SSD with PLP but why does it consume so much power for no reason???

MacOS Finder is fucking awful garbage

So, I drag a folder into the sidebar on Finder into my "Favorites".

Then I right-click on the folder and rename it. 

The name changes.

Then it immediately changes back.

When I right-click to rename it, it goes back to the new name. 

But as soon as I click away again, it goes back to the old name.

MacOS Finder is such fucking awful garbage.

Force browser to reload cached css files

Top hits for this on Google are useless stuff like Shift+Ctrl+Reload which doesn't work on my browsers.

Here is the actual fix: https://stackoverflow.com/questions/3466989/what-does-appending-v-1-to-css-and-javascript-urls-in-link-and-script-tags-do

You just append the ?v=x.x.x in your HTML and it magically gets the browser to reload the CSS file.

Pretty annoying that that isn't the top result on Google.

Thursday, September 5, 2024

MacOS is fucking awful

I fucking hate how bad MacOS is at preserving data.

I copied over a folder to another folder with the same name. It asked me if I want to overwrite, I said yes.

Now everything in the old folder is gone, WTF

I expected it to not remove files in the old folder, only copy over files from the new folder and overwrite if both have same names. 

This behavior is totally unexpected and makes no sense

Maybe I got too used to using tools with actually sane behavior like rsync

God I fucking hate MacOS so much. 

This must be the 3rd time I had catastrophic data loss on MacOS due to some design stupidity.