There was significant buzz around “Waitress” when it played at Sundance in January. Its writer and director, Adrienne Shelly, had been murdered two months earlier, and some of us wondered if the film was being picked up for distribution simply because it had that salacious behind-the-scenes story. Was it a curiosity, or was it actually a good movie?

Turns out it’s the latter. “Waitress” is not just good, but great, a funny and warm-hearted story about finding happiness, and the things people do while slogging through the valleys that sometimes lead to it. Shelly’s screenplay is witty, even poetic, and she demonstrates an enviable understanding of human nature and an affection for her characters. She clearly understood that sometimes the most joyful happy endings only come after enduring some trials.

The title character is Jenna (Keri Russell), and she has indeed suffered her share of tribulation. She is young and pretty and an expert maker of pies, the envy of her friends and co-workers at Joe’s Pie Diner in the little Southern town where the film is set. But no one would trade places with her for a minute, because she’s married to Earl (Jeremy Sisto), a stifling lout who only knows how to love her by controlling her. She can’t own a car or even keep her own tip money. He changed after they got married, and she hasn’t loved him in years.

And so Jenna is sad, trapped in a bad situation where her only hope is to squirrel away enough cash to leave Earl and start over somewhere else. And then: Dang it, she gets pregnant. Earl got her drunk a few weeks ago, her defenses were down, and yep, now she’s with child. She’s not going to abort it, and she’s going to take care of herself while she’s pregnant, but she has no interest in this baby. The motherly feelings that are supposed to kick in? Not happening.

Jenna’s lifeline is her two friends, both fellow waitresses at Joe’s. Becky (Cheryl Hines), the saucier one, is married to a never-seen old invalid and is constantly sparring with Cal (Lew Temple), the grouchy restaurant manager. Dawn (Adrienne Shelly herself) is single and trying not to let her native optimism give way to despair as she continues to get older without finding love. The three women love and support one another through all their various trials, and they make some fine-looking pies all the while. (If you love pie, this movie will be like porn for you. It’s pie porn.)

As if being married to a jerk and pregnant with a baby she doesn’t want weren’t enough, Jenna’s life becomes more complicated when she meets her OB-GYN, Dr. Pomatter (Nathan Fillion). He’s married, handsome, nervous, and a little odd. Before you know it, he and Jenna are having an affair.

Now, I want you to withhold judgment. One of the things I like about the film is that it doesn’t denounce its characters’ questionable behavior, nor does it condone it. It just lets them be who they are, flaws and all. Jenna knows it isn’t right for her, a married woman, to be seeing Dr. Pomatter, a married man. But she also knows that Dr. Pomatter listens to her and cares for her. When she’s with him, she feels happy and safe — something she hasn’t felt in a long, long time. No, the situation isn’t ideal, and no, the movie isn’t saying you should go out and commit some adultery if you think it’ll help you feel better. What it’s saying is that happiness is not always where you expect to find it, and that our lives can change for the better through ways we would not have foreseen.

Take Dawn, for example. She’s plain-looking and simple, a down-home girl with damaged self-esteem. She’s been doing what she calls “5-minute dates,” so that if the guy turns out to be a dud, she doesn’t waste a whole evening. (“Have fun on your 5-minute date!” Becky chirps. “Be sure to use a 5-minute condom!”) One dud she meets is named Ogie (Eddie Jemison), a sweet but unremarkable man who has one thing going for him: endless enthusiasm. Ever-smiling, he shows up at Joe’s Pie Diner the next day, insisting Dawn is the love of his life and he will never relent in his pursuit of her. He improvises poetry for her: “All my life I met harlots, but you are a queen/Ba-DA-duh-duh DA-duh-duh something between.” And you know what? Eventually he wins her over. Their romance is a sunny contrast to the love-gone-wrong that exists in Jenna and Earl’s home, a reminder that happiness is out there somewhere.

Nathan Fillion, best known for his roles in TV’s “Firefly” and the film it spawned, “Serenity,” is at his best when playing characters who are mildly befuddled, as Dr. Pomatter is. His rugged good looks make you think he’ll be suave and confident, and then his delivery reveals hilarious uncertainty. Many of this film’s funniest moments are the result of his interaction with Keri Russell — another TV actor (“Felicity”) whose charm and grace demand to be seen by wider audiences. You will absolutely fall in love with her in “Waitress.”

Let me also mention Old Joe. He’s a cantankerous old coot who owns the diner and several other local businesses, and he uses his grumpiness to hide his soft heart. He likes to sit in his favorite booth, eat his favorite pies, and complain. He is played by Andy Griffith, whom you probably already love for other reasons but who proves here that even at 80 years old, he can still do something surprising. Here’s a character that could easily be a cliche — the cranky old guy who offers sage advice — and instead he’s bracingly colorful and vivid.

Also crucial to the film’s success is Jeremy Sisto’s performance as Earl. Again, it would be easy to stereotype him: the villainous, no-good husband. Do too much to humanize him and you deny the audience the satisfaction of watching a bad guy get his comeuppance. But make him too one-dimensionally sinister and you lose the realism. Sisto’s performance strikes a delicate balance between the two. We catch enough details about his insecurities to see him as a plausible character, but not so much that he becomes sympathetic. We believe him AND we hate him — which, if you think about it, is a rare combination in movies.

But back to Jenna and the doctor. From a storytelling standpoint, Shelly has painted herself into a corner. We want Jenna and Dr. Pomatter to wind up together, but how can that happen in a way that has no collateral damage (we know Jenna’s husband’s a jerk, but what about Pommater’s wife?), and that is also plausible? All the believable outcomes don’t sound very happy, and all the happy outcomes don’t sound very believable.

What a marvel, then, that Shelly pulls it off. The ending is both realistic and happy — and I do mean happy, as in, make-you-cry, exit-the-theater-skipping-and-dancing, that kind of happy. The viewer and Jenna feel the same way: that it would be impossible for this to end well. And then, unexpectedly, it does, and having been so close to defeat makes the victory that much sweeter. What a tart, lovable, literate, life-affirming movie this is.

A (1 hr., 47 min.; PG-13, brief sexuality, some mild profanity.)