Monday, August 19, 2019

Timers that capture the execution context, evil AsyncLocal and the quick and ugly solution used by Microsoft devs

It all started with the source code for NonCapturingTimer, a static factory class that was creating a System.Threading.Timer without capturing the execution context and was described as "A convenience API for interacting with System.Threading.Timer in a way that doesn't capture the ExecutionContext. We should be using this (or equivalent) everywhere we use timers to avoid rooting any values stored in asynclocals.". What did that even mean?

An issue opened by David Fowler sheds some light on this: "Any lazy activation of timers will capture the ExecutionContext. Combining this with a lazy initialization of the HttpClient and the handler graph may end up holding onto AsyncLocals for longer than expected. This could end up looking like a memory leak". This follows a Twitter thread from Fowler declaring AsyncLocal as evil.

There are also multiple issues that have crystallized into a proposal for a future version of .NET: "Timer static Create methods that make rooting behavior explicit".

And if you look at the ASP.Net sources on GitHub, they do use the class mostly for one time timer calls and periodic cleanup calls. I should mention that Ben Adams from Microsoft calls this way of creating timers ugly.

I don't have the time to go down further on this rabbit hole, but maybe people will find answers here when looking into this and comment on their findings.

The Ten Thousand (The Macht #1), by Paul Kearney

book cover The Macht series is not related to anything Germanic, as the name might imply, but is instead inspired by the Greek Hoplites. Even if The Ten Thousand happens on another planet that has different continents and two moons and three intelligent races, Paul Kearney could have written it just as well as a historical novel set in ancient Greece, with Greek mercenaries getting hired by Persians as weapons in a civil war that went awry. In fact, the story really is inspired by an actual historical group called the Ten Thousand and the main character, with the unfortunate name of Rictus, seems to be inspired by that of real life Xenophon. The book seems to be a retelling of Anabasis.

Now, the story is well written and short. I read the whole thing in a day. It's like a 300 novel, with the courageous and lethal Macht force finding itself on a foreign continent, surrounded by overwhelming hostile forces and having to march through rough and deadly terrain in order to get home. There is a lot of fighting, technical and military, some romance, bro-mance and feudal politics, but it's essentially the story of a huge march seen through the eyes of an experienced soldier, but young and new to the mercenary troupe.

Bottom line: I don't know if I am going to read any other Macht book. This one felt self contained and I am not that much into ancient fights in the Bronze Age. I liked The Ten Thousand, though, and I recommend it as a short and captivating read.

The Luminous Dead, by Caitlin Starling

book cover The Luminous Dead has only two active characters: a cave diver, covered in a special suit that enhances her strength and completely isolates her from the environment, and her handler, the person who guides and provides support remotely. At first glance it's a sci-fi story, as it happens on another planet, with futuristic technology, different rules, alien lifeforms, etc. However, it starts to itch at you that with the tech that was described, the tasks at hand could have been completed a lot more efficiently and safely, so what gives?

Turns out the story is more of a metaphor than a fantastic cave adventure on another planet. Probably inspired by the death of Caitlin Starling's mother, it explores the damage done by losing your parents, the obsessions that drive the affected, the extent to which someone will go to quiet those voices in their heads. But I liked it. It's got just enough action and adrenaline to keep you going while it touches the painful emotional bits that the book was really about.

Bottom line: I urge you to ignore all technical aspects of the story. It's not that the author did not made the effort to make them believable, it's that they are irrelevant to the moral of the book. Also ignore the wild emotional fluctuations of the characters: they are supposed to behave that way. The book feels as if following a personal journal with the events of the story being just sci-fi versions of the items there.

Sunday, August 18, 2019

The giant house spider

While being hosted in a small house in the Arges county, I found myself face to... err... hairy body to hairy body with a large spider, at least 10 cm from the tips of the front legs to the tips of the hind legs. The body itself was about 2 cm in length. I tried to catch it, but it ran away and I didn't exactly feel safe handling it. However, from my standpoint, the adventure was just beginning. It starts with a picture:


In a previous post, I was telling you about a web site that recognized plants from blurry pictures I had taken. I assumed that somewhere there was something similar for spiders. Alas, it doesn't seem to be the case, so I reverted to the classic Google search with descriptive words: a European spider 10 cm in length. I found it almost immediately, clearly a member of the genus Tegenaria in the family Agelenidae. Or is it?

Funny enough, most of the spiders assigned to Tegenaria were recently (2013) moved to another genus, Eratigena, based on DNA and molecular analysis. So my spider, likely a "giant house spider", can be found either as Tegenaria atrica or Eratigena atrica (not to mention Tegenaria duellica, Tegenaria gigantea and Tegenaria saeva). I also can't be completely sure of the identification. As far as I can tell, it's a male, based on the large (pedi)palps, used not only to hold on to prey, but also to deposit sperm in the females. But while I think it looks most like an atrica, based on the pictures I found online, it could also be an Agelena labyrinthica or a Tegenaria parietina, even if the first is found in Denmark and should be smaller and the second is rare and native to Central Asia.

Considering it's a giant spider I found in a house, I will go with Eratigena atrica, but one has to wonder how active biology as a science is to have species of common arthropods reassigned from one genus to another just a few years ago. So yeah, a nice little story, for me at least, all starting from a picture of a spider in a bathroom.

Wednesday, August 14, 2019

Tate no Yuusha no Nariagari, or Rise of the Shield Hero. A ridiculous, but fun anime.

Tate no Yuusha no Nariagari is one of those isekai animes where a normal Japanese boy is summoned in a magical realm to fight monsters. Once there, he realizes that the world and his character work exactly as in a fantasy video game, complete with items with upgradeable stats, waves of monsters and revealing female armor. He is summoned there with three other heroes, also from Japan, only from alternate universes, each of the heroes having their own magical item that defines their style. His item is a shield and immediately he notices that he is treated differently, with all honors given to the other three and only disgust for him. Long story short, he is forced to hone his skills through his will and efforts alone, while the others, spoiled by their environment, make no effort and therefore level up less.

I liked this anime and I will continue to watch it, although it's a bit ridiculous. I've read the manga as well, which is also new, and there are slight differences in the sense that the anime is a little more serious. If you want a mindless game like experience in anime form, go for it. Here is a trailer:

Violet Evergarden, a really nice anime, with an uncertain future

Violet Evergarden is set in a steampunk universe in which technology, other than metal prosthetics, is at the 19th century level, and the main character is a girl that was used as an elite child soldier in a terrible war who now has to find a purpose in a civilian life. She takes on the job of a "auto memory doll", a person who needs to put into words the feelings of others. That's a bit of a stretch, because she doesn't know how to feel herself... it's like me taking on a job in psychology or artistic design so as to learn a new skill. Certainly great for me, but kind of sucks for my employer!

Anyway, the animation is really well done and the acting is top notch. The story itself is beautiful, even if at times inconsistent. After watching the 14 episodes of the first season, I was itching for more, only to hear from a colleague that the studio responsible for the animation, Kyoto Animation, was destroyed in a terrible arson attack. That doesn't bode well for a sequel, yet a spin-off film had already been announced, so who knows?

Bottom line: it's not for everyone. PTSD romance, I would call it. But it nicely animated and I liked the story. I felt that the characters were a bit off, but not annoyingly so. Here is a trailer, in English:

Tuesday, August 13, 2019

The Binding, by Bridget Collins

book cover There is a trope in fantasy writing that goes like this: young person is uprooted from their life as they learn a new skill or gain a new power, while people in authority teach them things so slowly and inefficiently that the young person needs to make efforts to learn by themselves. Harry Potter had it. The Binding has it. Only in the case of this book, the young person is someone who is always sick without knowing why, is suddenly sent off from home to be an apprentice for someone he doesn't know and while he understands nothing of what he is supposed to do. Months later and 25% of the book in, he still hasn't learnt anything, although he is always tired and exhausted for no good reason whenever he is about to. If he doesn't collapse, his mentor will! And, spoilers ahead, his mentor then dies.

So after a quarter of the book, absolutely nothing happened while I had to suffer a depressed weak and barely teenage boy who didn't want, know, or want to know anything. Bridget Collins' writing is competent and I am sure things are going to get better from here (I mean, they can hardly get worse), but I just won't continue to read the book.

Saturday, August 10, 2019

Magic for Liars, by Sarah Gailey

book cover Magic for Liars is one of those stories where magic exists in a hidden layer of our world, yet it doesn't bring any happiness. People are still people, regardless of their power. And what better way to explore human nature than writing a detective story in a high school for magical kids?

Sarah Gailey writes the story from the viewpoint of the detective, a woman who's greatest frustrations stem from her sister being a magician, while she is just "normal". Yet when she is tasked to find out the circumstances of the death of one of the teachers in the magic school, she jumps on it, making her realize more about herself and the relationships with family and other people.

Yes, the magic is quite incidental and the detective part quite secondary and it could just as well been written in a space academy or science lab or anywhere where flawed people have to manage each other and the balance of power between them. And while I feel the main character was compelling and the story well written, I can't quite shake the feeling I've been duped into reading a touchy-feely type of drama that I didn't really intend to read. You will not read about the specifics of magic in any kind of way; it is quite bluntly used as a tool that the reader needs not understand. You will not be amazed by the amazing feats of deduction of a fascinating detective; the main character is by definition a very normal person with a penchant for introspection and focusing on her own messed up feelings.

In the end you know the spouse did it, or the butler, or the person who flirts with the detective, or the god-like magician, or the weird kid. The culprit isn't even that important for the plot. It's all about the theoretical dynamics between the people and what makes them tick.

Bottom line: a fine investigation into the core motivations of people, woven in a rather short and bland story which purely incidentally features detectives and magic.

Friday, August 09, 2019

When is efficiency bad? TED talk: The human skills we need in an unpredictable world, from Margaret Heffernan

The talk is a bit overreaching, doing more things than it should have tried to do, in other words not very efficient, but it contains some really interesting ideas and it's extremely well articulated. As a software developer, I always thrive for efficiency, but what Margaret Heffernan says is that efficiency is good only when the future is predictable. In a fast changing world (and getting faster), efficiency doesn't just not help, it makes things worse. Sometimes you can't plan, but you can prepare.

As expected, I wasn't as interested in the humanistic part of the talk as I was in the technical aspects: algorithmic efficiency is only good with things that can be measured. If you want to innovate or to adapt, it's trial and error that works best. I got that giddiness I get when people tell me "you can't do THAT in software!". Oh, ye of little faith! But there is something there, something extremely useful, a tool that I can use whenever I get stuck in logic loops or some manager tells me that he needs data in order to make a decision about the project that would generate the data.


Margaret Heffernan: The human skills we need in an unpredictable world

Tuesday, August 06, 2019

.NET Core: Cannot resolve scoped service 'Microsoft.Extensions.Options.IOptionsSnapshot...' from root provider

I was trying to create an instance of an object from a service provider to resolve any dependencies, using ActivatorUtilities.CreateInstance<MyObject>(_serviceProvider) and I was getting the exception:
System.InvalidOperationException
  HResult=0x80131509
  Message=Cannot resolve scoped service 'Microsoft.Extensions.Options.IOptionsSnapshot`1[ExternalConfiguration]' from root provider.

My object was receiving a parameter of type IOptionsSnapshot<ExternalConfiguration> and upon further investigation, my service provider (which came as a resolution from the dependency injection for IServiceProvider) was actually a ServiceProviderEngineScope which just refused to resolve any IOptionsSnapshot! Funny enough, if I replaced IOptionsSnapshot with IOptionsMonitor, which in my mind is a heavier interface, it worked without issues. Further still, the problem appeared only inside an IHostedService (a BackgroundService hooked up with services.AddHostedService<T>); if I wrote the same code in a controller action, for instance, it worked fine.

The .NET 2+ implementation of IOptionsSnapshot<T> is OptionsManager<T>. If I manually resolved an instance of OptionsManager before my object, then added it as a parameter, the code worked:
var optionsSnapshot = ActivatorUtilities.CreateInstance<OptionsManager<TestOptions>>(_serviceProvider);
var myObject = ActivatorUtilities.CreateInstance<MyObject>(_serviceProvider, optionsSnapshot);

So, specifically, the issue is that in .NET Core, the service provider implementation cannot resolve IOptionsSnapshot interfaces in worker services. You can still do that manually, but I suspect it is a bug, since there is no problem using an IOptionsMonitor instead of IOptionsSnapshot.

A possible solution is to use an additional service provider only for IOptionsSnapshot. Warning, this will not work in a general situation if the dependencies from the additional service provider also need parameters that would be found in the original service provider:

// initialization code
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(
  typeof(IOptionsSnapshot<>), 
  typeof(OptionsManager<>)
);
serviceCollection.AddSingleton(
  typeof(IOptionsFactory<>),
  typeof(OptionsFactory<>)
);
_additionalServiceProvider = serviceCollection.BuildServiceProvider();
 
// resolution code
var constructor = typeof(MyObject).GetConstructors()
  .Where(ci=>ci.IsPublic)
  .Single();
var args = constructor.GetParameters()
    .Select(p =>
    {
        try
        {
            return _serviceProvider.GetService(p.ParameterType);
        }
        catch
        {
            return _additionalServiceProvider.GetService(p.ParameterType);
        }
    })
    .ToArray();
return ActivatorUtilities.CreateInstance<MyObject>(_serviceProvider, args);

Monday, August 05, 2019

Blackfish City, by Sam J. Miller

book cover There are writers like Steven Erikson, describing worlds so vast that characters seem to drown in them, there are writers like William Gibson or Charles Stross, who go so far into the future that people seem to lose their relevance, yet none of them dare to ignore their characters or fail to make them interesting. Yet, that's what Sam J. Miller does in Blackfish City.

10% in and I couldn't bare reading any more of this book. I couldn't care less about the asexual person that is whining about his life, I couldn't care less about the orphan girl whining about her life, I couldn't care less about the sick homosexual boy who is whining about his life and I couldn't care less about the anonymous radio show that narrated what the city was instead of the action showing me. Should I care about all the world getting sunk under the water until the only livable places are floating megacities reminiscent of Waterworld and run by semi abandoned AIs? Should I care about the artificial drama, weird futuristic disease or the grey whining world that Miller describes? No. I refuse!

Bottom line: a poor man's cyberpunk story, with the mechanical "woke", but irrelevant to anything else, sexual and cultural references, with boring characters and a story that I had to make an effort to wade through and still didn't seem to go anywhere.

Sunday, August 04, 2019

City of Miracles (The Divine Cities #3), by Robert Jackson Bennett

book cover It takes the third book to see that the "hero's journey" is actually Sigrud's, even if he is the lead character only in this final book of the Divine Cities series. And yes, it is the final book, with a satisfying and very permanent ending, with no hope of dragging it on. Hell, if you consider there are three books and two cities and no gods left, you could wonder how would anyone continue the story if pressured by publishing companies.

Robert Jackson Bennett did not disappoint with City of Miracles, switching registers a little by turning the usual steampunk-noir detective story into more of a chase and revenge thriller. The villain is revealed quite soon, the mystery split into multiple little quirks that nag at you until the end of the book and a massive divine battle to top it all up. Yet I was a little disappointed with the ending. I know, rationally, that it is a great ending, but emotionally I didn't get what I needed from it, especially as I was still fired up on the penultimate chapter only to get an "aftermath" chapter for last.

Maybe it was that one of the options they employed at the end could have easily been the first and solved a lot of problems to boot. Or the fact that a lot of the grief in the final third of the book came from Sigrud not checking his kills, which is something he would never ever do. It felt a little stretched and tired compared to the other stories.

Sometimes I wonder, is it easier or harder for a writer to just abandon a world that he so carefully crafted? Is it a burden that everything that is successful needs to be turned into a trilogy or a series, or is it like coming home, writing about good friends living in your head? Either way, I am kind of grateful to Bennett for ending it all. I would have read more and more books like these if he wrote them, because I am an addict at heart, but I believe both of us can do better.

Bottom line: It's kind of difficult to compare the three books in the series. The first one was the most captivating, but also first, the second one was darker, yet in the same vein, while the last felt philosophical and like the writer wanted to get it done with. You can read either as standalone, although it makes more sense to read the whole trilogy from first to last. I recommend it and I will read more from the author.